Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PartialsDir or PartialsGlob #5

Open
kravemir opened this issue Oct 26, 2019 · 7 comments
Open

Support for PartialsDir or PartialsGlob #5

kravemir opened this issue Oct 26, 2019 · 7 comments

Comments

@kravemir
Copy link

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?

@foolin
Copy link
Owner

foolin commented Oct 29, 2019

Use include tag:

{{include "layouts/footer"}}

The path is:

$root/layouts/footer.html

@kravemir
Copy link
Author

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

@foolin
Copy link
Owner

foolin commented Oct 30, 2019

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

@foolin
Copy link
Owner

foolin commented Oct 30, 2019

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

@kravemir
Copy link
Author

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

@foolin
Copy link
Owner

foolin commented Oct 30, 2019

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

@kravemir
Copy link
Author

kravemir commented Nov 6, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants