Skip to content

Commit

Permalink
feat(tpl): add cachedPartial template function
Browse files Browse the repository at this point in the history
- allows to specify that a partial should be cached after first
  evaluation, useful for static complex partials often used partials
  • Loading branch information
menski committed Jul 11, 2016
1 parent 7212e51 commit e15f8f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tpl/template.go
Expand Up @@ -31,6 +31,8 @@ import (

var localTemplates *template.Template
var tmpl Template
var cachedPartials map[string]template.HTML


// TODO(bep) an interface with hundreds of methods ... remove it.
// And unexport most of these methods.
Expand Down Expand Up @@ -92,6 +94,7 @@ func New() Template {
}

localTemplates = &templates.Template
cachedPartials = make(map[string]template.HTML)

for k, v := range funcMap {
amber.FuncMap[k] = v
Expand All @@ -115,6 +118,14 @@ func partial(name string, contextList ...interface{}) template.HTML {
return ExecuteTemplateToHTML(context, "partials/"+name, "theme/partials/"+name)
}

func cachedPartial(name string, context_list ...interface{}) template.HTML {
if _, found := cachedPartials[name]; !found {
cachedPartials[name] = partial(name, context_list...)
}

return cachedPartials[name]
}

func executeTemplate(context interface{}, w io.Writer, layouts ...string) {
worked := false
for _, layout := range layouts {
Expand Down
1 change: 1 addition & 0 deletions tpl/template_funcs.go
Expand Up @@ -1795,6 +1795,7 @@ func init() {
"mul": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '*') },
"ne": ne,
"partial": partial,
"cachedPartial": cachedPartial,
"plainify": plainify,
"pluralize": pluralize,
"readDir": readDirFromWorkingDir,
Expand Down

4 comments on commit e15f8f5

@LiaungYip
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a feature request open for this on the main Hugo project - see:

A PR might be well received!

@menski
Copy link
Author

@menski menski commented on e15f8f5 Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I known but this is more like a hack and I wasn't comfortable to submit it upstream. Also I probably miss some go knowledge on how to do this in a proper way. Maybe if I have some spare time I will try to submit something useful.

@moorereason
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@menski,
I suspect that your solution is susceptible to race conditions. You need to protect the map accesses with a mutex. Have a look at the regexpCache type to see how I handled a similar situation when trying to cache regular expression objects.

Another addition that would help get this accepted upstream is to add some simple unit tests.

@menski
Copy link
Author

@menski menski commented on e15f8f5 Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tips. I will have a look at the regex cache.

Please sign in to comment.