mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
tpl: Rework to handle both text and HTML templates
Before this commit, Hugo used `html/template` for all Go templates. While this is a fine choice for HTML and maybe also RSS feeds, it is painful for plain text formats such as CSV, JSON etc. This commit fixes that by using the `IsPlainText` attribute on the output format to decide what to use. A couple of notes: * The above requires a nonambiguous template name to type mapping. I.e. `/layouts/_default/list.json` will only work if there is only one JSON output format, `/layouts/_default/list.mytype.json` will always work. * Ambiguous types will fall back to HTML. * Partials inherits the text vs HTML identificator of the container template. This also means that plain text templates can only include plain text partials. * Shortcode templates are, by definition, currently HTML templates only. Fixes #3221
This commit is contained in:
@@ -177,7 +177,7 @@ var isInnerShortcodeCache = struct {
|
||||
// to avoid potential costly look-aheads for closing tags we look inside the template itself
|
||||
// we could change the syntax to self-closing tags, but that would make users cry
|
||||
// the value found is cached
|
||||
func isInnerShortcode(t *template.Template) (bool, error) {
|
||||
func isInnerShortcode(t tpl.TemplateExecutor) (bool, error) {
|
||||
isInnerShortcodeCache.RLock()
|
||||
m, ok := isInnerShortcodeCache.m[t.Name()]
|
||||
isInnerShortcodeCache.RUnlock()
|
||||
@@ -188,10 +188,7 @@ func isInnerShortcode(t *template.Template) (bool, error) {
|
||||
|
||||
isInnerShortcodeCache.Lock()
|
||||
defer isInnerShortcodeCache.Unlock()
|
||||
if t.Tree == nil {
|
||||
return false, errors.New("Template failed to compile")
|
||||
}
|
||||
match, _ := regexp.MatchString("{{.*?\\.Inner.*?}}", t.Tree.Root.String())
|
||||
match, _ := regexp.MatchString("{{.*?\\.Inner.*?}}", t.Tree())
|
||||
isInnerShortcodeCache.m[t.Name()] = match
|
||||
|
||||
return match, nil
|
||||
@@ -398,8 +395,6 @@ Loop:
|
||||
case tScName:
|
||||
sc.name = currItem.val
|
||||
tmpl := getShortcodeTemplate(sc.name, p.s.Tmpl)
|
||||
{
|
||||
}
|
||||
if tmpl == nil {
|
||||
return sc, fmt.Errorf("Unable to locate template for shortcode %q in page %q", sc.name, p.Path())
|
||||
}
|
||||
@@ -570,7 +565,10 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin
|
||||
return source, nil
|
||||
}
|
||||
|
||||
func getShortcodeTemplate(name string, t tpl.Template) *template.Template {
|
||||
func getShortcodeTemplate(name string, t tpl.TemplateHandler) *tpl.TemplateAdapter {
|
||||
isInnerShortcodeCache.RLock()
|
||||
defer isInnerShortcodeCache.RUnlock()
|
||||
|
||||
if x := t.Lookup("shortcodes/" + name + ".html"); x != nil {
|
||||
return x
|
||||
}
|
||||
@@ -580,7 +578,7 @@ func getShortcodeTemplate(name string, t tpl.Template) *template.Template {
|
||||
return t.Lookup("_internal/shortcodes/" + name + ".html")
|
||||
}
|
||||
|
||||
func renderShortcodeWithPage(tmpl *template.Template, data *ShortcodeWithPage) string {
|
||||
func renderShortcodeWithPage(tmpl tpl.Template, data *ShortcodeWithPage) string {
|
||||
buffer := bp.GetBuffer()
|
||||
defer bp.PutBuffer(buffer)
|
||||
|
||||
|
Reference in New Issue
Block a user