goview
goview copied to clipboard
Support for `PartialsDir` or `PartialsGlob`
Golang's native html supports template.ParseGlob
, which I find quite useful. And for example, another template engine, extemplate, supports ParseDir
.
Currently, goview requires all partials to be listed in goview.Config
. Would it be possible to add support to include partials recursively from specific base directory? Or, include partials matching specific glob?
Use include tag:
{{include "layouts/footer"}}
The path is:
$root/layouts/footer.html
@foolin thank you for the answer.
I have tried include
tag, but it doesn't support passing arguments to template, but it doesn't work:
{{include "category_tree" .CategoryTree}}
Above fails with error:
ViewEngine execute template error: template: catalogue:8:22: executing "content" at <include>: wrong number of args for include: want 1 got 2
While, use of template works:
{{template "category_tree" .CategoryTree}}
{{include}} tag is shared the page context arguments, and you don't need passing arguments. So you can just use it.
Example:
GO code:
//render page use `page.tpl` with '.tpl' will only file template without master layout.
http.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
err := goview.Render(w, http.StatusOK, "page.tpl", goview.M{
"Title": "Page file title!!",
})
if err != nil {
fmt.Fprintf(w, "Render page.html error: %v!", err)
}
})
page.tpl
<!-- /views/page.html -->
<!doctype html>
<html>
<head>
<title>{{.Title}}</title>
{{include "layouts/head"}}
</head>
<body>
<a href="/"><- Back home!</a>
{{template "ad" .}}
<hr>
{{include "layouts/footer"}}
</body>
</html>
layouts/footer.tpl
This page title: {{.Title}}
The variable {{.Title}} is shared with {{include}} tag.
https://github.com/foolin/goview/tree/master/_examples/advance
Actually, category_tree
was a bad example, as, coincidentally, there's only one category tree on page. I'm sorry for misleading you with that example.
The more relevant example is photo
component (I haven't created it yet, but I'm planning to do it soon). The photo
component displays a thumb (small jpg image) with link to full size image, and applies correct HTML classes. Advanced version of photo component would support also some more customization.
The photo
component will be used in following way:
{{template "category_tree" path.to.photo.view.model}}
Oh, you wan't the config Partials support for directory or matching specific files, right?
Oh, you wan't the config Partials support for directory or matching specific files, right?
Yes. I would like to have glob matching support for partials config, like:
Partials: []string{"partials/**"},