Skip to content

Commit

Permalink
feat(template): remove pongo2 to html/template
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Nov 3, 2023
1 parent 59255e4 commit c268bbf
Show file tree
Hide file tree
Showing 68 changed files with 533 additions and 1,237 deletions.
1 change: 0 additions & 1 deletion core/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
var (
ErrUnableToFindEmbed = errors.New("unable to find the embed file/directory")
ErrModuleDoesNotExist = errors.New("module does not exist")
ErrInvalidPongoRef = errors.New("invalid pongo reference name")
ErrInvalidTemplateRef = errors.New("invalid template reference name")
)

Expand Down
58 changes: 25 additions & 33 deletions core/embed/embed_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"html/template"

"github.com/flosch/pongo2"
"github.com/rande/goapp"
"github.com/rande/gonode/core/config"
log "github.com/sirupsen/logrus"
Expand All @@ -18,28 +17,11 @@ func Configure(l *goapp.Lifecycle, conf *config.Config) {
return NewEmbeds()
})

return nil
})

l.Register(func(app *goapp.App) error {
app.Set("gonode.pongo", func(app *goapp.App) interface{} {
engine := pongo2.NewSet("gonode.embeds", &PongoTemplateLoader{
Embeds: app.Get("gonode.embeds").(*Embeds),
BasePath: "",
})

engine.Options = &pongo2.Options{
TrimBlocks: true,
LStripBlocks: true,
}

return engine
})

app.Set("gonode.template", func(app *goapp.App) interface{} {
return &TemplateLoader{
Embeds: app.Get("gonode.embeds").(*Embeds),
BasePath: "",
FuncMap: map[string]interface{}{},
}
})

Expand All @@ -51,15 +33,13 @@ func Configure(l *goapp.Lifecycle, conf *config.Config) {
return nil
}

// expose files using static/modules/[path]

mux := app.Get("goji.mux").(*web.Mux)
logger := app.Get("logger").(*log.Logger)
asset := app.Get("gonode.embeds").(*Embeds)
loader := app.Get("gonode.template").(*TemplateLoader)
embeds := app.Get("gonode.embeds").(*Embeds)

loader.Templates = GetTemplates(embeds)
loader.Templates = GetTemplates(embeds, loader.FuncMap)

ConfigureEmbedMux(mux, asset, "/static", logger)

Expand All @@ -68,12 +48,32 @@ func Configure(l *goapp.Lifecycle, conf *config.Config) {
}

// This function is called only once at boot time to configure the different template
func GetTemplates(embeds *Embeds) map[string]*template.Template {
func GetTemplates(embeds *Embeds, funcMap map[string]interface{}) map[string]*template.Template {
entries := embeds.GetFilesByExt(".html")
// in the entries we need to find the page, each page will have its own set of templates (layout, blocks, etc ...)

templates := map[string]*template.Template{}

formPath := "templates/form/"
for _, entry := range entries {
if len(entry.Path) < len(formPath) || entry.Path[0:len(formPath)] != formPath {
continue
}

name := fmt.Sprintf("%s:%s", entry.Module, entry.Path[10:len(entry.Path)-5])

if data, err := embeds.ReadFile(entry.Module, entry.Path); err != nil {
fmt.Printf("Unable to read file: %s\n", err)
panic(err)
} else {
templates[name] = template.New(name).Funcs(funcMap)
_, err := templates[name].Parse(string(data))
if err != nil {
panic(err)
}
}
}

// create root template without parsing them
pagesPath := "templates/pages/"
for _, entry := range entries {
Expand All @@ -82,25 +82,21 @@ func GetTemplates(embeds *Embeds) map[string]*template.Template {
}

name := fmt.Sprintf("%s:%s", entry.Module, entry.Path[10:len(entry.Path)-5])
templates[name] = template.New(name)
templates[name] = template.New(name).Funcs(funcMap)
}

layoutsPath := "templates/layouts/"
blocksPath := "templates/blocks/"

// load all the layout first, default templates will be defined
for name, tpl := range templates {

fmt.Printf("Iterating over layout: %s\n", name)
for _, tpl := range templates {
for _, entry := range entries {
if len(entry.Path) < len(layoutsPath) || entry.Path[0:len(layoutsPath)] != layoutsPath {
continue
}

name := fmt.Sprintf("%s:%s", entry.Module, entry.Path[10:len(entry.Path)-5])

fmt.Printf("Loading layout: %s\n", name)

if data, err := embeds.ReadFile(entry.Module, entry.Path); err != nil {
fmt.Printf("Unable to read file: %s\n", err)
panic(err)
Expand All @@ -112,17 +108,13 @@ func GetTemplates(embeds *Embeds) map[string]*template.Template {

// load all the blocks first, so this will let an option to overwrite them if needed in
// the page

fmt.Printf("Iterating over block: %s\n", name)
for _, entry := range entries {
if len(entry.Path) < len(blocksPath) || entry.Path[0:len(blocksPath)] != blocksPath {
continue
}

name := fmt.Sprintf("%s:%s", entry.Module, entry.Path[10:len(entry.Path)-5])

fmt.Printf("Loading blocks: %s\n", name)

if data, err := embeds.ReadFile(entry.Module, entry.Path); err != nil {
fmt.Printf("Unable to read file: %s\n", err)
panic(err)
Expand Down
52 changes: 0 additions & 52 deletions core/embed/embed_pongo.go

This file was deleted.

96 changes: 0 additions & 96 deletions core/embed/embed_pongo_test.go

This file was deleted.

6 changes: 4 additions & 2 deletions core/embed/embed_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ package embed
import (
"bytes"
"errors"
"fmt"
"html/template"
)

var (
ErrRootTemplateNotFound = errors.New("root template not found")
)

type Context map[string]interface{}
type FuncMap map[string]interface{}

type TemplateLoader struct {
Embeds *Embeds
BasePath string
Templates map[string]*template.Template
FuncMap map[string]interface{}
}

// Abs calculates the path to a given template. Whenever a path must be resolved
Expand All @@ -42,7 +45,6 @@ func (l *TemplateLoader) Abs(base, name string) string {
func (l *TemplateLoader) Execute(path string, data interface{}) ([]byte, error) {
var buf bytes.Buffer

fmt.Printf("Execute: %s\n", path)
if tpl, ok := l.Templates[path]; !ok {
return nil, ErrRootTemplateNotFound
} else if err := tpl.ExecuteTemplate(&buf, path, data); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/embed/embed_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_Template_With_Custom_Path(t *testing.T) {

var buf bytes.Buffer

templates := GetTemplates(embeds)
templates := GetTemplates(embeds, map[string]interface{}{})
ctx := map[string]interface{}{
"Title": "Hello World!",
}
Expand Down
42 changes: 0 additions & 42 deletions core/form/__snapshots__/form_pongo_test.snap

This file was deleted.

Loading

0 comments on commit c268bbf

Please sign in to comment.