goview icon indicating copy to clipboard operation
goview copied to clipboard

Support for `PartialsDir` or `PartialsGlob`

Open kravemir opened this issue 5 years ago • 7 comments

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?

kravemir avatar Oct 26 '19 09:10 kravemir

Use include tag:

{{include "layouts/footer"}}

The path is:

$root/layouts/footer.html

foolin avatar Oct 29 '19 02:10 foolin

@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}}

kravemir avatar Oct 29 '19 13:10 kravemir

{{include}} tag is shared the page context arguments, and you don't need passing arguments. So you can just use it.

foolin avatar Oct 30 '19 07:10 foolin

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

foolin avatar Oct 30 '19 07:10 foolin

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}}

kravemir avatar Oct 30 '19 08:10 kravemir

Oh, you wan't the config Partials support for directory or matching specific files, right?

foolin avatar Oct 30 '19 09:10 foolin

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/**"},

kravemir avatar Nov 06 '19 16:11 kravemir