fileFixer: func(content string) string {
return strings.Replace(content, ".Title }}", ".Title }", 1)
},
- assertCreateError: func(a testSiteBuildErrorAsserter, err error) {
+ // Base templates gets parsed at build time.
+ assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
a.assertLineNumber(4, err)
},
},
a.c.Assert(fe.Position().LineNumber, qt.Equals, 5)
a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 1)
a.c.Assert(fe.ChromaLexer, qt.Equals, "go-html-template")
- a.assertErrorMessage("\"layouts/_default/single.html:5:1\": parse failed: template: _default/single.html.___b:5: unexpected \"}\" in operand", fe.Error())
+ a.assertErrorMessage("\"layouts/foo/single.html:5:1\": parse failed: template: foo/single.html:5: unexpected \"}\" in operand", fe.Error())
},
},
baseof: make(map[string]templateInfo),
needsBaseof: make(map[string]templateInfo),
- main: newTemplateNamespace(funcMap, false),
+ main: newTemplateNamespace(funcMap),
Deps: d,
layoutHandler: output.NewLayoutHandler(),
return e, nil
}
-func newTemplateNamespace(funcs map[string]interface{}, lock bool) *templateNamespace {
- var mu *sync.RWMutex
- if lock {
- mu = &sync.RWMutex{}
- }
-
+func newTemplateNamespace(funcs map[string]interface{}) *templateNamespace {
return &templateNamespace{
prototypeHTML: htmltemplate.New("").Funcs(funcs),
prototypeText: texttemplate.New("").Funcs(funcs),
templateStateMap: &templateStateMap{
- mu: mu,
templates: make(map[string]*templateState),
},
}
t.applyTemplateTransformers(t.main, ts)
+ if err := t.extractPartials(ts.Template); err != nil {
+ return nil, false, err
+ }
+
return ts, true, nil
}
if isBaseTemplatePath(name) {
// Store it for later.
t.baseof[name] = tinfo
- // Also parse and add it on its own to make sure we reach the inline partials.
- tinfo.name = name + ".___b"
- _, err := t.addTemplateTo(tinfo, t.main)
- if err != nil {
- return tinfo.errWithFileContext("parse failed", err)
- }
return nil
}
needsBaseof := !t.noBaseNeeded(name) && needsBaseTemplate(tinfo.template)
if needsBaseof {
t.needsBaseof[name] = tinfo
- // Also parse and add it on its own to make sure we reach the inline partials.
- tinfo.name = name + ".___b"
- _, err := t.addTemplateTo(tinfo, t.main)
- if err != nil {
- return tinfo.errWithFileContext("parse failed", err)
- }
return nil
}
return strings.Contains(name, "_markup/")
}
+func (t *templateHandler) extractPartials(templ tpl.Template) error {
+ templs := templates(templ)
+ for _, templ := range templs {
+ if templ.Name() == "" || !strings.HasPrefix(templ.Name(), "partials/") {
+ continue
+ }
+
+ ts := newTemplateState(templ, templateInfo{name: templ.Name()})
+ ts.typ = templatePartial
+
+ t.main.mu.RLock()
+ _, found := t.main.templates[templ.Name()]
+ t.main.mu.RUnlock()
+
+ if !found {
+ t.main.mu.Lock()
+ // This is a template defined inline.
+ _, err := applyTemplateTransformers(ts, t.main.newTemplateLookup(ts))
+ if err != nil {
+ t.main.mu.Unlock()
+ return err
+ }
+ t.main.templates[templ.Name()] = ts
+ t.main.mu.Unlock()
+
+ }
+ }
+
+ return nil
+
+}
+
func (t *templateHandler) postTransform() error {
defineCheckedHTML := false
defineCheckedText := false
defineCheckedHTML = true
}
- templs := templates(v.Template)
- for _, templ := range templs {
- if templ.Name() == "" || !strings.HasPrefix(templ.Name(), "partials/") {
- continue
- }
-
- ts := newTemplateState(templ, templateInfo{name: templ.Name()})
- ts.typ = templatePartial
-
- if _, found := t.main.templates[templ.Name()]; !found {
- // This is a template defined inline.
-
- _, err := applyTemplateTransformers(ts, t.main.newTemplateLookup(ts))
- if err != nil {
- return err
- }
- t.main.templates[templ.Name()] = ts
-
- }
+ if err := t.extractPartials(v.Template); err != nil {
+ return err
}
}
*templateStateMap
}
-func (t templateNamespace) Clone(lock bool) *templateNamespace {
- if t.mu != nil {
- t.mu.Lock()
- defer t.mu.Unlock()
- }
-
- var mu *sync.RWMutex
- if lock {
- mu = &sync.RWMutex{}
- }
+func (t templateNamespace) Clone() *templateNamespace {
+ t.mu.Lock()
+ defer t.mu.Unlock()
t.templateStateMap = &templateStateMap{
templates: make(map[string]*templateState),
- mu: mu,
}
t.prototypeText = texttemplate.Must(t.prototypeText.Clone())
}
func (t *templateNamespace) Lookup(name string) (tpl.Template, bool) {
- if t.mu != nil {
- t.mu.RLock()
- defer t.mu.RUnlock()
- }
+ t.mu.RLock()
+ defer t.mu.RUnlock()
templ, found := t.templates[name]
if !found {
return nil, false
}
- if t.mu != nil {
- return &templateWrapperWithLock{RWMutex: t.mu, Template: templ}, true
- }
-
return templ, found
}
}
func (t *templateNamespace) parse(info templateInfo) (*templateState, error) {
- if t.mu != nil {
- t.mu.Lock()
- defer t.mu.Unlock()
- }
+ t.mu.Lock()
+ defer t.mu.Unlock()
if info.isText {
prototype := t.prototypeText
}
type templateStateMap struct {
- mu *sync.RWMutex // May be nil
+ mu sync.RWMutex
templates map[string]*templateState
}