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 )
0 commit comments