Skip to content

Commit c268bbf

Browse files
committed
feat(template): remove pongo2 to html/template
1 parent 59255e4 commit c268bbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+533
-1237
lines changed

core/embed/embed.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
var (
1616
ErrUnableToFindEmbed = errors.New("unable to find the embed file/directory")
1717
ErrModuleDoesNotExist = errors.New("module does not exist")
18-
ErrInvalidPongoRef = errors.New("invalid pongo reference name")
1918
ErrInvalidTemplateRef = errors.New("invalid template reference name")
2019
)
2120

core/embed/embed_app.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"html/template"
66

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

21-
return nil
22-
})
23-
24-
l.Register(func(app *goapp.App) error {
25-
app.Set("gonode.pongo", func(app *goapp.App) interface{} {
26-
engine := pongo2.NewSet("gonode.embeds", &PongoTemplateLoader{
27-
Embeds: app.Get("gonode.embeds").(*Embeds),
28-
BasePath: "",
29-
})
30-
31-
engine.Options = &pongo2.Options{
32-
TrimBlocks: true,
33-
LStripBlocks: true,
34-
}
35-
36-
return engine
37-
})
38-
3920
app.Set("gonode.template", func(app *goapp.App) interface{} {
4021
return &TemplateLoader{
4122
Embeds: app.Get("gonode.embeds").(*Embeds),
4223
BasePath: "",
24+
FuncMap: map[string]interface{}{},
4325
}
4426
})
4527

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

54-
// expose files using static/modules/[path]
55-
5636
mux := app.Get("goji.mux").(*web.Mux)
5737
logger := app.Get("logger").(*log.Logger)
5838
asset := app.Get("gonode.embeds").(*Embeds)
5939
loader := app.Get("gonode.template").(*TemplateLoader)
6040
embeds := app.Get("gonode.embeds").(*Embeds)
6141

62-
loader.Templates = GetTemplates(embeds)
42+
loader.Templates = GetTemplates(embeds, loader.FuncMap)
6343

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

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

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

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

57+
formPath := "templates/form/"
58+
for _, entry := range entries {
59+
if len(entry.Path) < len(formPath) || entry.Path[0:len(formPath)] != formPath {
60+
continue
61+
}
62+
63+
name := fmt.Sprintf("%s:%s", entry.Module, entry.Path[10:len(entry.Path)-5])
64+
65+
if data, err := embeds.ReadFile(entry.Module, entry.Path); err != nil {
66+
fmt.Printf("Unable to read file: %s\n", err)
67+
panic(err)
68+
} else {
69+
templates[name] = template.New(name).Funcs(funcMap)
70+
_, err := templates[name].Parse(string(data))
71+
if err != nil {
72+
panic(err)
73+
}
74+
}
75+
}
76+
7777
// create root template without parsing them
7878
pagesPath := "templates/pages/"
7979
for _, entry := range entries {
@@ -82,25 +82,21 @@ func GetTemplates(embeds *Embeds) map[string]*template.Template {
8282
}
8383

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

8888
layoutsPath := "templates/layouts/"
8989
blocksPath := "templates/blocks/"
9090

9191
// load all the layout first, default templates will be defined
92-
for name, tpl := range templates {
93-
94-
fmt.Printf("Iterating over layout: %s\n", name)
92+
for _, tpl := range templates {
9593
for _, entry := range entries {
9694
if len(entry.Path) < len(layoutsPath) || entry.Path[0:len(layoutsPath)] != layoutsPath {
9795
continue
9896
}
9997

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

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

113109
// load all the blocks first, so this will let an option to overwrite them if needed in
114110
// the page
115-
116-
fmt.Printf("Iterating over block: %s\n", name)
117111
for _, entry := range entries {
118112
if len(entry.Path) < len(blocksPath) || entry.Path[0:len(blocksPath)] != blocksPath {
119113
continue
120114
}
121115

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

124-
fmt.Printf("Loading blocks: %s\n", name)
125-
126118
if data, err := embeds.ReadFile(entry.Module, entry.Path); err != nil {
127119
fmt.Printf("Unable to read file: %s\n", err)
128120
panic(err)

core/embed/embed_pongo.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

core/embed/embed_pongo_test.go

Lines changed: 0 additions & 96 deletions
This file was deleted.

core/embed/embed_template.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ package embed
88
import (
99
"bytes"
1010
"errors"
11-
"fmt"
1211
"html/template"
1312
)
1413

1514
var (
1615
ErrRootTemplateNotFound = errors.New("root template not found")
1716
)
1817

18+
type Context map[string]interface{}
19+
type FuncMap map[string]interface{}
20+
1921
type TemplateLoader struct {
2022
Embeds *Embeds
2123
BasePath string
2224
Templates map[string]*template.Template
25+
FuncMap map[string]interface{}
2326
}
2427

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

45-
fmt.Printf("Execute: %s\n", path)
4648
if tpl, ok := l.Templates[path]; !ok {
4749
return nil, ErrRootTemplateNotFound
4850
} else if err := tpl.ExecuteTemplate(&buf, path, data); err != nil {

core/embed/embed_template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Test_Template_With_Custom_Path(t *testing.T) {
2020

2121
var buf bytes.Buffer
2222

23-
templates := GetTemplates(embeds)
23+
templates := GetTemplates(embeds, map[string]interface{}{})
2424
ctx := map[string]interface{}{
2525
"Title": "Hello World!",
2626
}

core/form/__snapshots__/form_pongo_test.snap

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)