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
Add a global Twig variable with Contao state #6436
Conversation
I think the downside of something like this is, that the controller rendering the template no longer knows what data the template accesses, making the response of that controller theoretically uncacheable. However, that problem already exists because of the global Maybe we could at least mention that in the documentation of this global twig variable then? |
We also should discuss if this would better be a runtime that provides dedicated functions instead of a 'global'. I'm not sure yet but I'm leaning a bit towards the latter because then we could provide some more convenience instead of outputting raw values that the template has to deal with. @aschempp Out of interest: where did you need those things? I assume in content elements where you did not have control over the controllers? If so, we could also just add these things to |
I mostly used them in In my cases, neither of these would affect the cache (since anything in regards to user being logged in means a session, and a session means no cache). And any page property wouldn't change on a per-user basis. |
At least the |
It would probably be better to add a |
Servus & Moin! Thanks for your great work! Greetings from Munich, |
I agree that we should add this (or any other global variables that are useful) to the new LTS. Could also be done as a bugfix. |
# Conflicts: # core-bundle/config/services.yaml # core-bundle/src/Twig/Extension/ContaoExtension.php
PR updated. I also think we should "group" more useful global properties into the I checked the |
Also, the |
The properties are now {{ contao.has_backend_user }}
{{ contao.is_preview_mode }}
{{ contao.request_token }}
{{ contao.page.* }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Apart from #6436 (comment)
a921337
to
3043873
Compare
Thank you @aschempp. |
I think we controversely discussed this before. However, I had to copy-paste this to multiple of my projects recently, and I think it would make sense in general. With this, you can do the following in any Twig template:
any property from the page object (from current request = usually the global)
check if preview mode is enabled