-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
Can't get twig filter in macros #578
Comments
I can confirm this bug, I am using this trick on a link macro to mark the link as the currently visited one: {% macro dropdownLink(href, label, target = '_self') %}
<li>
<a href="{{ href }}"
target="{{ target }}"
class="dark:hover:bg-jacarta-600 hover:text-accent focus:text-accent hover:bg-jacarta-50 flex content-center rounded-xl px-5 py-3 transition-colors text-lg font-display text-sm leading-normal
{% if (''|page == href) %}text-accent-dark dark:text-white{% else %}text-jacarta-700 dark:text-jacarta-200{% endif %}
">
<span class="leading-none">{{ label }}</span>
</a>
</li>
{% endmacro %} This issue occurred during migration from 1.1.8 to 1.2. |
I made some investigations: Doing so caused the CMS environment to not "overlaps" the Twig's one, making the I made a dirty test to convince myself, replacing the twig package line linked previously by this one: ->write("\$context = \$this->env->mergeGlobals(array_merge(['this' => 'test'],\n")
// And another tiny-modification after that just to close the parenthesis Doing so make the I looked into the Twig documentation and found how to add a global variable. I was able to fix this issue by modifying the protected function initTwigEnvironment()
{
$this->twig = App::make('twig.environment.cms');
$this->twig->addGlobal('this', ['controller' => $this]);
} Unfortunately, I am not sure if this is the most efficient way to do, and if some other global variable should be injected to prevent other related issue? |
Good find @RomainMazB ! Whether or not the global var is the right fix, at least we have a good pointer on where the problem lies. |
@LukeTowers I confirm this resolves the problem I was having in this discussion: https://discord.com/channels/816852513684193281/816852817267785758/988861851582464000 |
@LukeTowers @bennothommo @jaxwilko can you give your thoughts about the suggested fix from @RomainMazB above? I can submit a PR for it in the morning. Thanks. |
On the outset it looks fine, but Luke's original PR was trying to avoid too many global attributes polluting the Twig environment. And I'm presuming that the |
@mjauvin, actually I agree with @bennothommo on the fact that this is probably not the right fix (I would have directly submit a PR if I was not convinced of that). But I disagree with @bennothommo too when he says that the macro share the same scope that the page or layout. The macro node doesn't even go through the To me a good fix would be to create a custom This is more or less how the partial node works, and so does the content and component nodes (without any context for contents and the component context for the components). |
Hmm, sounds interesting @RomainMazB, as long as @bennothommo doesn't have any objections I'd be interested in seeing a PR that implements those suggestions. Sounds like we need to also expand our testing suite a bit in this area as well. |
I'll wait for Ben's feedback before working on this. I could submit a PR including tests this week. |
@bennothommo can you give your thoughts on this so that we can resolve this? Thanks! |
I'm happy for you guys to proceed with a PR. Might have a couple of comments then, but will be better to test things in action. |
…ig macros. This fixes #578 by adding the ability to pass the CMS Controller instance to the CMS Twig Extension removing the reliance on context variables as well as making the expected "global" twig variables inside of the CMS Twig environment actually global within that environment. Replaces #598 & #593. Credit to @RomainMazB for the initial implementation.
@damsfx @RomainMazB @mjauvin @bennothommo I believe I've come up with an acceptable fix in 5b8d189, please test and verify |
Well, looks like we have a winner! This resolve the issue with the DynamicPDF plugin as well. |
This issue is still present in the 1.1.9 branch, as well as #557 . Still need to downgrade twig/twig from v2.15.1 to v2.14.3. |
@damsfx which part of the issue? There were a few different bugs at play here, what error are you getting? |
@LukeTowers the issue due to calling The only one remaining is calling the
|
@damsfx apply wintercms/storm@1b866f0 and see if that fixes it. If it does then feel free to submit a PR to the 1.1 branch backporting that temporary fix and you can use the dev-1.1 version in your composer.json to pull it in after the PR is merged. |
Done : |
Winter CMS Build
1.2
PHP Version
8.0
Database engine
MySQL/MariaDB
Plugins installed
No response
Issue description
Getting an error when using
|page
twig filter in a macro.An exception has been thrown during the rendering of a template ("Undefined array key "this"").
Steps to replicate
composer create-project wintercms/winter winter12 "dev-wip/1.2 as 1.2" php artisan winter:install php artisan winter:env php artisan winter:up
Then add
{{ 'plugins' | page }}
in default layout or home page.Getting link to plugins page is ok.
If the same filter is call from within a macro, it fails.
Add a macro to layout and calling it :
Fall in an error.
Workaround
No response
The text was updated successfully, but these errors were encountered: