diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-06 20:15:28 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-13 22:44:15 +0300 |
commit | af72db806f2c1c0bf1dfe5832275c41eeba89906 (patch) | |
tree | a1bc9c7d09836073a811a1b395b7756727fdb210 /tpl | |
parent | e951d65771ca299aa899e91bfe00411a5ada8f19 (diff) |
hugolib: Handle shortcode per output format
This commit allows shortcode per output format, a typical use case would be the special AMP media tags.
Note that this will only re-render the "overridden" shortcodes and only in pages where these are used, so performance in the normal case should not suffer.
Closes #3220
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/template.go | 5 | ||||
-rw-r--r-- | tpl/tplimpl/template.go | 22 |
2 files changed, 27 insertions, 0 deletions
diff --git a/tpl/template.go b/tpl/template.go index 9fbf6b7b8..aa46a8ac2 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -58,6 +58,11 @@ type TemplateExecutor interface { Tree() string } +// TemplateDebugger prints some debug info to stdoud. +type TemplateDebugger interface { + Debug() +} + // TemplateAdapter implements the TemplateExecutor interface. type TemplateAdapter struct { Template diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 77826e0b0..ae1ae6820 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -14,7 +14,9 @@ package tplimpl import ( + "fmt" "html/template" + "path" "strings" texttemplate "text/template" @@ -39,6 +41,7 @@ const ( var ( _ tpl.TemplateHandler = (*templateHandler)(nil) + _ tpl.TemplateDebugger = (*templateHandler)(nil) _ tpl.TemplateFuncsGetter = (*templateHandler)(nil) _ tpl.TemplateTestMocker = (*templateHandler)(nil) _ tpl.TemplateFinder = (*htmlTemplates)(nil) @@ -88,6 +91,11 @@ func (t *templateHandler) addError(name string, err error) { t.errors = append(t.errors, &templateErr{name, err}) } +func (t *templateHandler) Debug() { + fmt.Println("HTML templates:\n", t.html.t.DefinedTemplates()) + fmt.Println("\n\nText templates:\n", t.text.t.DefinedTemplates()) +} + // PrintErrors prints the accumulated errors as ERROR to the log. func (t *templateHandler) PrintErrors() { for _, e := range t.errors { @@ -293,6 +301,13 @@ func (t *htmlTemplates) addTemplateIn(tt *template.Template, name, tpl string) e return err } + if strings.Contains(name, "shortcodes") { + // We need to keep track of one ot the output format's shortcode template + // without knowing the rendering context. + withoutExt := strings.TrimSuffix(name, path.Ext(name)) + tt.AddParseTree(withoutExt, templ.Tree) + } + return nil } @@ -315,6 +330,13 @@ func (t *textTemplates) addTemplateIn(tt *texttemplate.Template, name, tpl strin return err } + if strings.Contains(name, "shortcodes") { + // We need to keep track of one ot the output format's shortcode template + // without knowing the rendering context. + withoutExt := strings.TrimSuffix(name, path.Ext(name)) + tt.AddParseTree(withoutExt, templ.Tree) + } + return nil } |