Skip to content

Commit

Permalink
Merge branch '74-add-blocks-to-the-template-engine' into 'master'
Browse files Browse the repository at this point in the history
Resolve "Add blocks to the template engine"

Closes #74

See merge request cploutarchou/MicroGO!46
  • Loading branch information
cploutarchou committed Jul 31, 2022
2 parents 8aef50e + d9546c3 commit ccff59e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 51 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/jackc/pgx/v4 v4.13.0
github.com/joho/godotenv v1.4.0
github.com/justinas/nosurf v1.1.1
github.com/kataras/blocks v0.0.6
github.com/ory/dockertest/v3 v3.8.0
github.com/robfig/cron/v3 v3.0.1
github.com/vanng822/go-premailer v1.20.1
Expand Down Expand Up @@ -85,8 +86,10 @@ require (
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/vanng822/css v1.0.1 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ github.com/justinas/nosurf v1.1.1/go.mod h1:ALpWdSbuNGy2lZWtyXdjkYv4edL23oSEgfBT
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v2.3.0+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/kataras/blocks v0.0.6 h1:UQI+2AMxhUoe5WcyAT0AdPHTSMcrPy+ALAgvYj2vPwo=
github.com/kataras/blocks v0.0.6/go.mod h1:UK+Iwk0Oxpc0GdoJja7sEildotAUKK1LYeYcVF0COWc=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand Down Expand Up @@ -496,6 +498,8 @@ github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OK
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
Expand Down Expand Up @@ -545,6 +549,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/unrolled/render v1.0.3/go.mod h1:gN9T0NhL4Bfbwu8ann7Ry/TGHYfosul+J0obPf6NBdM=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/vanng822/css v1.0.1 h1:10yiXc4e8NI8ldU6mSrWmSWMuyWgPr9DZ63RSlsgDw8=
github.com/vanng822/css v1.0.1/go.mod h1:tcnB1voG49QhCrwq1W0w5hhGasvOg+VQp9i9H1rCM1w=
github.com/vanng822/go-premailer v1.20.1 h1:2LTSIULXxNV5IOB5BSD3dlfOG95cq8qqExtRZMImTGA=
Expand Down
13 changes: 8 additions & 5 deletions microGo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/gomodule/redigo/redis"
"github.com/joho/godotenv"
"github.com/kataras/blocks"
"github.com/robfig/cron/v3"
"log"
"net/http"
Expand Down Expand Up @@ -45,6 +46,7 @@ type MicroGo struct {
Routes *chi.Mux
Render *render.Render
JetView *jet.Set
BlocksView *blocks.Blocks
config config
Session *scs.SessionManager
DB Database
Expand Down Expand Up @@ -287,11 +289,12 @@ func (m *MicroGo) startLoggers() (*log.Logger, *log.Logger, *log.Logger, *log.Lo
//createRenderer Create a Renderer for microGo application.
func (m *MicroGo) createRenderer() {
renderer := render.Render{
Renderer: m.config.renderer,
RootPath: m.RootPath,
Port: m.config.port,
JetViews: m.JetView,
Session: m.Session,
Renderer: m.config.renderer,
RootPath: m.RootPath,
Port: m.config.port,
JetViews: m.JetView,
BlocksViews: m.BlocksView,
Session: m.Session,
}
m.Render = &renderer
}
Expand Down
52 changes: 44 additions & 8 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import (
"github.com/CloudyKit/jet/v6"
"github.com/alexedwards/scs/v2"
"github.com/justinas/nosurf"
"github.com/kataras/blocks"
"html/template"
"log"
"net/http"
"strings"
"time"
)

type Render struct {
Renderer string
RootPath string
Secure bool
Port string
ServerName string
JetViews *jet.Set
Session *scs.SessionManager
Renderer string
RootPath string
Secure bool
Port string
ServerName string
JetViews *jet.Set
BlocksViews *blocks.Blocks
Session *scs.SessionManager
}

type TemplateData struct {
Expand Down Expand Up @@ -50,12 +53,16 @@ func (r *Render) DefaultData(templateData *TemplateData, request *http.Request)
}

// Page The page render function. You can use it to render pages using go or jet templates.
func (r *Render) Page(writer http.ResponseWriter, request *http.Request, view string, variables, data interface{}) error {
func (r *Render) Page(writer http.ResponseWriter, request *http.Request, view, layout string, variables, data interface{}) error {
switch strings.ToLower(r.Renderer) {
case "go":
return r.GoPage(writer, request, view, data)
case "jet":
return r.JetPage(writer, request, view, variables, data)
case "blocks":
data := data.(map[string]interface{})
return r.BlocksPage(writer, request, view, layout, nil, data)

default:
}
return errors.New("No rendering engine available. Please fill the required value (go or jet) in .env file ")
Expand Down Expand Up @@ -106,3 +113,32 @@ func (r *Render) JetPage(writer http.ResponseWriter, request *http.Request, view
}
return nil
}

//BlocksPage The Blocks' engine template renderer function.
func (r *Render) BlocksPage(writer http.ResponseWriter, request *http.Request, view, layout string, variables interface{}, data map[string]interface{}) error {
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
r.BlocksViews = blocks.New("./views").
Reload(true).
Funcs(map[string]interface{}{
"year": func() int {
return time.Now().Year()
},
})
err := r.BlocksViews.Load()
if err != nil {
return err
}

td := &TemplateData{}

if data != nil {

data["data"] = r.DefaultData(td, request)
}

err = r.BlocksViews.ExecuteTemplate(writer, view, layout, data)
if err != nil {
return err
}
return nil
}
24 changes: 21 additions & 3 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ var templateTests = []struct {
},
{"jetTemplateNoTemplate", "jet", "not-exists",
true, "Something went wrong. Unable to render a not existing jet template.",
}, {"blocksTemplate", "blocks", "index",
false, "Unable to render jet template.",
},
{"blocksTemplateNoTemplate", "blocks", "not-exists",
true, "Something went wrong. Unable to render a not existing jet template.",
},
{"invalidRenderEngine", "foo", "home",
true, "No error while trying to render template with no valid engine.",
Expand All @@ -41,7 +46,7 @@ func TestRenderPage(t *testing.T) {

testRenderer.Renderer = task.renderer
testRenderer.RootPath = "./test"
err = testRenderer.Page(w, r, task.template, nil, nil)
err = testRenderer.Page(w, r, task.template, "", nil, nil)

if task.errorExpected {
if err == nil {
Expand All @@ -66,7 +71,7 @@ func TestRenderGoPage(t *testing.T) {
}
testRenderer.Renderer = "go"
testRenderer.RootPath = "./test"
err = testRenderer.Page(w, r, "home", nil, nil)
err = testRenderer.Page(w, r, "home", "", nil, nil)
if err != nil {
t.Error("Unable to render Go template. ", err)
}
Expand All @@ -79,8 +84,21 @@ func TestRenderJetPage(t *testing.T) {
}
testRenderer.Renderer = "jet"
testRenderer.RootPath = "./test"
err = testRenderer.Page(w, r, "home", nil, nil)
err = testRenderer.Page(w, r, "home", "", nil, nil)
if err != nil {
t.Error("Unable to render Jet template. ", err)
}
}
func TestRenderBlocksPage(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/url", nil)
if err != nil {
t.Error(err)
}
testRenderer.Renderer = "blocks"
testRenderer.RootPath = "./test"
err = testRenderer.Page(w, r, "index", "main", nil, nil)
if err != nil {
t.Error("Unable to render blocks template. ", err)
}
}
16 changes: 13 additions & 3 deletions render/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ package render

import (
"github.com/CloudyKit/jet/v6"
"github.com/kataras/blocks"
"os"
"testing"
"time"
)

var views = jet.NewSet(
jet.NewOSFileSystemLoader("./test/views"),
jet.InDevelopmentMode(),
)
var blocksViews = blocks.New("./test/views").
Reload(true).
Funcs(map[string]interface{}{
"year": func() int {
return time.Now().Year()
},
})

var testRenderer = Render{
Renderer: "",
RootPath: "",
JetViews: views,
Renderer: "",
RootPath: "",
JetViews: views,
BlocksViews: blocksViews,
}

func TestMain(m *testing.M) {
Expand Down
13 changes: 0 additions & 13 deletions render/test/views/home.jet

This file was deleted.

13 changes: 0 additions & 13 deletions render/test/views/home.page.tmpl

This file was deleted.

12 changes: 6 additions & 6 deletions terminal/cli/templates/env.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Give your application a unique name (no spaces)
APP_NAME=${APP_NAME}
APP_NAME=test
APP_URL=http://localhost:4000

# false for production, true for development
Expand Down Expand Up @@ -28,13 +28,13 @@ DATABASE_TIME_ZONE=UTC
REDIS_HOST="
REDIS_PORT="6379"
REDIS_PASSWORD=
REDIS_PREFIX=${APP_NAME}
REDIS_PREFIX=test

# cache (Supported cache databases redis/badger)
CACHE=badger

# cooking settings
COOKIE_NAME=${APP_NAME}
COOKIE_NAME=test
COOKIE_LIFETIME=1
COOKIE_PERSIST=true
COOKIE_SECURE=false
Expand All @@ -58,8 +58,8 @@ MAILER_API=
MAILER_KEY=
MAILER_URL=

# template engine: go or jet
RENDERER=jet
# template engine: go/blocks/jet
RENDERER=blocks

# the encryption key; must be exactly 32 characters long
ENCRYPTION_KEY=${KEY}
ENCRYPTION_KEY=4ff4D44d4s5fsw6D64D4Df4f47d44fw5
3 changes: 3 additions & 0 deletions terminal/cli/templates/go.mod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/jackc/pgx/v4 v4.13.0
github.com/joho/godotenv v1.4.0
github.com/justinas/nosurf v1.1.1
github.com/kataras/blocks v0.0.6
github.com/ory/dockertest/v3 v3.8.0
github.com/robfig/cron/v3 v3.0.1
github.com/vanng822/go-premailer v1.20.1
Expand Down Expand Up @@ -85,8 +86,10 @@ require (
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/vanng822/css v1.0.1 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
Expand Down

0 comments on commit ccff59e

Please sign in to comment.