Skip to content

Commit

Permalink
use consts only in New()
Browse files Browse the repository at this point in the history
  • Loading branch information
raspi committed Aug 31, 2020
1 parent 5183df5 commit 76f4777
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ type pageCache map[language.Tag]map[string]*template.Template

// Template contains HTML used as a base and all the pages that are using that base
type Template struct {
baseTemplateFuncs template.FuncMap // Global functions for baseTemplateContents template
baseTemplateContents string // Base template as string
pages map[string]page // Pages which uses baseTemplateContents template
pagesCached pageCache // template cache for faster processing
addPageDefine bool // add {{define ...}} to pages automatically?
pageDefineName string // name for define in pages
baseDefineName string // name for define in base
baseTemplateFuncs template.FuncMap // Global functions for baseTemplateContents template
baseTemplateContents string // Base template as string
pages map[string]page // Pages which uses baseTemplateContents template
pagesCached pageCache // template cache for faster processing
addPageDefine bool // add {{define ...}} to pages automatically?
pageDefineName string // name for define in pages
baseDefineName string // name for define in base
translationFunctionName string // name for translation token function
}

// New creates translatable HTML pages
func New(baseContents string, funcs template.FuncMap) *Template {
t := &Template{
pages: make(map[string]page),
pagesCached: make(pageCache),
baseTemplateFuncs: funcs, // functions available to all pages
addPageDefine: true,
baseDefineName: baseDefineName,
pageDefineName: pageDefineName,
pages: make(map[string]page),
pagesCached: make(pageCache),
baseTemplateFuncs: funcs, // functions available to all pages
addPageDefine: true,
baseDefineName: baseDefineName,
pageDefineName: pageDefineName,
translationFunctionName: translationFunctionName,
}

t.baseTemplateContents = `{{define "` + t.baseDefineName + `"}}` + baseContents + `{{end}}`
Expand Down Expand Up @@ -110,7 +112,7 @@ func (t *Template) getTemplate(templatename string, language language.Tag) (tpl

_ = tpl.Funcs(template.FuncMap{
// Translate inside template
translationFunctionName: func(s string, a ...interface{}) template.HTML {
t.translationFunctionName: func(s string, a ...interface{}) template.HTML {
pr := message.NewPrinter(language, message.Catalog(pageTemplate.trcat))
return template.HTML(pr.Sprintf(s, a...))
},
Expand Down Expand Up @@ -139,7 +141,7 @@ func (t *Template) Render(w io.Writer, templatename string, language language.Ta
return fmt.Errorf(`couldn't get template %q: %w`, templatename, err)
}

err = tpl.ExecuteTemplate(w, baseDefineName, data)
err = tpl.ExecuteTemplate(w, t.baseDefineName, data)
if err != nil {
return fmt.Errorf(`couldn't execute template %q: %w`, templatename, err)
}
Expand Down

0 comments on commit 76f4777

Please sign in to comment.