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 global variables? #35

Closed
flosch opened this issue Sep 18, 2014 · 5 comments
Closed

Support global variables? #35

flosch opened this issue Sep 18, 2014 · 5 comments
Assignees

Comments

@flosch
Copy link
Owner

flosch commented Sep 18, 2014

Support global (static) variables across multiple template executions.

See #32 as well.

@digitalcrab
Copy link
Contributor

Hello, please take a look at the new commit 979c9d6

@flosch
Copy link
Owner Author

flosch commented Sep 19, 2014

I'm still not sure whether it's a good idea to make those variables generally available to any template you ever render with pongo2 within your application. Imagine you use pongo2 at another unrelated place in your application and you still have these variables registered to these unrelated templates. Wouldn't it be better to have a mechanism to make this variables only available to a subset of templates? I thought about introducing template groups which share common settings and variables. This would allow to create, for example, templates for mails which share a common set of global variables and to create a different set of templates for the web which share a different common set of globals (and other settings we can't imagine yet).

@flosch
Copy link
Owner Author

flosch commented Oct 1, 2014

Will work on that soon by introducing template groups/sets.

@flosch flosch self-assigned this Oct 1, 2014
@flosch flosch closed this as completed in 4ef4c2c Oct 1, 2014
@flosch
Copy link
Owner Author

flosch commented Oct 1, 2014

You can now either use pongo2.Globals to share a Global context among all templates within the default template set (which is created at pongo2 startup) or you can create your own template sets and set Globals for them individually:

set := pongo2.NewSet("our web templates") // The idea behind sets is that you can create another set with other globals and configurations for mail templates or other kind of templates
set.Globals["global_variable"] = "this is a test"
tpl, _ := set.FromFile("....")
out, _ := tpl.Execute(Context{"other_variables": 123})
...

More information about the recent change is in the commit message. Please let me know if this works for you.

@flosch flosch mentioned this issue Oct 1, 2014
@coditori
Copy link

This's not working on Pongo2. But this is working very well :)

tpl := pongo2.Must(pongo2.FromFile("your template address"))
pongo2.Globals["global_variable"] = "this is a test"
out, _ := tpl.Execute(Context{"other_variables": 123})
fmt.Fprintf(w, out)

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

No branches or pull requests

3 participants