diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-15 09:37:30 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-16 10:10:01 +0100 |
commit | ffaec4ca8c4c6fd05b195879ccd65acf2fd5a6ac (patch) | |
tree | 28d4a68d5b41d5ccd769208856d0e3b4f086ebe5 /tpl/lang | |
parent | 91fb8f1b59cce50de914d66dac1d406655c3c43b (diff) |
Add a way to merge pages by language
As an example:
```html
{{ $pages := .Site.RegularPages | lang.Merge $frSite.RegularPages | lang.Merge $enSite.RegularPages }}
```
Will "fill in the gaps" in the current site with, from left to right, content from the French site, and lastly the English.
Fixes #4463
Diffstat (limited to 'tpl/lang')
-rw-r--r-- | tpl/lang/lang.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/tpl/lang/lang.go b/tpl/lang/lang.go index 3a659b119..b29b182ab 100644 --- a/tpl/lang/lang.go +++ b/tpl/lang/lang.go @@ -15,6 +15,7 @@ package lang import ( "errors" + "fmt" "math" "strconv" "strings" @@ -134,3 +135,15 @@ func (ns *Namespace) NumFmt(precision, number interface{}, options ...interface{ return string(b), nil } + +type pagesLanguageMerger interface { + MergeByLanguageInterface(other interface{}) (interface{}, error) +} + +func (ns *Namespace) Merge(p2, p1 interface{}) (interface{}, error) { + merger, ok := p1.(pagesLanguageMerger) + if !ok { + return nil, fmt.Errorf("language merge not supported for %T", p1) + } + return merger.MergeByLanguageInterface(p2) +} |