mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Add render template hooks for links and images
This commit also * revises the change detection for templates used by content files in server mode. * Adds a Page.RenderString method Fixes #6545 Fixes #4663 Closes #6043
This commit is contained in:
@@ -15,14 +15,17 @@ package tplimpl
|
||||
import (
|
||||
"strings"
|
||||
|
||||
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
||||
"github.com/gohugoio/hugo/hugofs/files"
|
||||
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gohugoio/hugo/tpl"
|
||||
template "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
||||
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/identity"
|
||||
"github.com/gohugoio/hugo/tpl"
|
||||
)
|
||||
|
||||
// Issue #2927
|
||||
@@ -33,7 +36,7 @@ func TestTransformRecursiveTemplate(t *testing.T) {
|
||||
{{ define "menu-nodes" }}
|
||||
{{ template "menu-node" }}
|
||||
{{ end }}
|
||||
{{ define "menu-node" }}
|
||||
{{ define "menu-nßode" }}
|
||||
{{ template "menu-node" }}
|
||||
{{ end }}
|
||||
{{ template "menu-nodes" }}
|
||||
@@ -41,12 +44,25 @@ func TestTransformRecursiveTemplate(t *testing.T) {
|
||||
|
||||
templ, err := template.New("foo").Parse(recursive)
|
||||
c.Assert(err, qt.IsNil)
|
||||
parseInfo := tpl.DefaultParseInfo
|
||||
|
||||
ctx := newTemplateContext(createParseTreeLookup(templ))
|
||||
ctx := newTemplateContext(
|
||||
newTemplateInfo("test").(identity.Manager),
|
||||
&parseInfo,
|
||||
createGetTemplateInfoTree(templ.Tree),
|
||||
)
|
||||
ctx.applyTransformations(templ.Tree.Root)
|
||||
|
||||
}
|
||||
|
||||
func createGetTemplateInfoTree(tree *parse.Tree) func(name string) *templateInfoTree {
|
||||
return func(name string) *templateInfoTree {
|
||||
return &templateInfoTree{
|
||||
tree: tree,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type I interface {
|
||||
Method0()
|
||||
}
|
||||
@@ -80,13 +96,10 @@ func TestInsertIsZeroFunc(t *testing.T) {
|
||||
{{ with .TimeZero }}.TimeZero1 with: {{ . }}{{ else }}.TimeZero1 with: FALSE{{ end }}
|
||||
{{ template "mytemplate" . }}
|
||||
{{ if .T.NonEmptyInterfaceTypedNil }}.NonEmptyInterfaceTypedNil: TRUE{{ else }}.NonEmptyInterfaceTypedNil: FALSE{{ end }}
|
||||
|
||||
{{ template "other-file-template" . }}
|
||||
|
||||
{{ define "mytemplate" }}
|
||||
{{ if .TimeZero }}.TimeZero1: mytemplate: TRUE{{ else }}.TimeZero1: mytemplate: FALSE{{ end }}
|
||||
{{ end }}
|
||||
|
||||
`
|
||||
|
||||
// https://github.com/gohugoio/hugo/issues/5865
|
||||
@@ -97,7 +110,7 @@ func TestInsertIsZeroFunc(t *testing.T) {
|
||||
)
|
||||
|
||||
d := newD(c)
|
||||
h := d.Tmpl.(tpl.TemplateManager)
|
||||
h := d.Tmpl.(*templateHandler)
|
||||
|
||||
// HTML templates
|
||||
c.Assert(h.AddTemplate("mytemplate.html", templ1), qt.IsNil)
|
||||
@@ -107,15 +120,13 @@ func TestInsertIsZeroFunc(t *testing.T) {
|
||||
c.Assert(h.AddTemplate("_text/mytexttemplate.txt", templ1), qt.IsNil)
|
||||
c.Assert(h.AddTemplate("_text/myothertexttemplate.txt", templ2), qt.IsNil)
|
||||
|
||||
c.Assert(h.MarkReady(), qt.IsNil)
|
||||
c.Assert(h.markReady(), qt.IsNil)
|
||||
|
||||
for _, name := range []string{"mytemplate.html", "mytexttemplate.txt"} {
|
||||
var sb strings.Builder
|
||||
tt, _ := d.Tmpl.Lookup(name)
|
||||
sb := &strings.Builder{}
|
||||
|
||||
err := d.Tmpl.Execute(tt, sb, ctx)
|
||||
err := h.Execute(tt, &sb, ctx)
|
||||
c.Assert(err, qt.IsNil)
|
||||
|
||||
result := sb.String()
|
||||
|
||||
c.Assert(result, qt.Contains, ".True: TRUE")
|
||||
@@ -138,14 +149,10 @@ func TestCollectInfo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tplString string
|
||||
expected tpl.Info
|
||||
expected tpl.ParseInfo
|
||||
}{
|
||||
{"Basic Inner", `{{ .Inner }}`, tpl.Info{IsInner: true, Config: tpl.DefaultConfig}},
|
||||
{"Basic config map", "{{ $_hugo_config := `" + configStr + "` }}", tpl.Info{
|
||||
Config: tpl.Config{
|
||||
Version: 42,
|
||||
},
|
||||
}},
|
||||
{"Basic Inner", `{{ .Inner }}`, tpl.ParseInfo{IsInner: true, Config: tpl.DefaultParseConfig}},
|
||||
{"Basic config map", "{{ $_hugo_config := `" + configStr + "` }}", tpl.ParseInfo{Config: tpl.ParseConfig{Version: 42}}},
|
||||
}
|
||||
|
||||
echo := func(in interface{}) interface{} {
|
||||
@@ -162,12 +169,13 @@ func TestCollectInfo(t *testing.T) {
|
||||
|
||||
templ, err := template.New("foo").Funcs(funcs).Parse(test.tplString)
|
||||
c.Assert(err, qt.IsNil)
|
||||
parseInfo := tpl.DefaultParseInfo
|
||||
|
||||
ctx := newTemplateContext(createParseTreeLookup(templ))
|
||||
ctx := newTemplateContext(
|
||||
newTemplateInfo("test").(identity.Manager), &parseInfo, createGetTemplateInfoTree(templ.Tree))
|
||||
ctx.typ = templateShortcode
|
||||
ctx.applyTransformations(templ.Tree.Root)
|
||||
|
||||
c.Assert(ctx.Info, qt.Equals, test.expected)
|
||||
c.Assert(ctx.parseInfo, qt.DeepEquals, &test.expected)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -205,7 +213,10 @@ func TestPartialReturn(t *testing.T) {
|
||||
templ, err := template.New("foo").Funcs(funcs).Parse(test.tplString)
|
||||
c.Assert(err, qt.IsNil)
|
||||
|
||||
_, err = applyTemplateTransformers(templatePartial, templ.Tree, createParseTreeLookup(templ))
|
||||
_, err = applyTemplateTransformers(
|
||||
templatePartial,
|
||||
&templateInfoTree{tree: templ.Tree, info: tpl.DefaultParseInfo},
|
||||
createGetTemplateInfoTree(templ.Tree))
|
||||
|
||||
// Just check that it doesn't fail in this test. We have functional tests
|
||||
// in hugoblib.
|
||||
@@ -215,3 +226,10 @@ func TestPartialReturn(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func newTemplateInfo(name string) tpl.Info {
|
||||
return tpl.NewInfo(
|
||||
identity.NewManager(identity.NewPathIdentity(files.ComponentFolderLayouts, name)),
|
||||
tpl.DefaultParseInfo,
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user