From 03c36af2f97cb4ced34394a909d304cc08b4f366 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Fri, 23 Nov 2018 12:14:37 -0500 Subject: [PATCH] Optimize template loading on non-post pages Avoids loading render.tmpl where it isn't needed. --- templates.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/templates.go b/templates.go index aceea75..faf4379 100644 --- a/templates.go +++ b/templates.go @@ -49,23 +49,19 @@ func initTemplate(name string) { log.Info(" %s%s%s.tmpl", templatesDir, string(filepath.Separator), name) } + files := []string{ + filepath.Join(templatesDir, name+".tmpl"), + filepath.Join(templatesDir, "include", "footer.tmpl"), + filepath.Join(templatesDir, "base.tmpl"), + } if name == "collection" || name == "collection-tags" { // These pages list out collection posts, so we also parse templatesDir + "include/posts.tmpl" - templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles( - filepath.Join(templatesDir, name+".tmpl"), - filepath.Join(templatesDir, "include", "posts.tmpl"), - filepath.Join(templatesDir, "include", "footer.tmpl"), - filepath.Join(templatesDir, "include", "render.tmpl"), - filepath.Join(templatesDir, "base.tmpl"), - )) - } else { - templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles( - filepath.Join(templatesDir, name+".tmpl"), - filepath.Join(templatesDir, "include", "footer.tmpl"), - filepath.Join(templatesDir, "include", "render.tmpl"), - filepath.Join(templatesDir, "base.tmpl"), - )) + files = append(files, filepath.Join(templatesDir, "include", "posts.tmpl")) + } + if name == "collection" || name == "collection-tags" || name == "collection-post" || name == "post" { + files = append(files, filepath.Join(templatesDir, "include", "render.tmpl")) } + templates[name] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...)) } func initPage(path, key string) { @@ -76,7 +72,6 @@ func initPage(path, key string) { pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles( path, filepath.Join(templatesDir, "include", "footer.tmpl"), - filepath.Join(templatesDir, "include", "render.tmpl"), filepath.Join(templatesDir, "base.tmpl"), )) } @@ -90,7 +85,6 @@ func initUserPage(path, key string) { path, filepath.Join(templatesDir, "user", "include", "header.tmpl"), filepath.Join(templatesDir, "user", "include", "footer.tmpl"), - filepath.Join(templatesDir, "include", "render.tmpl"), )) }