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

Fix twig environment #593

Closed
wants to merge 3 commits into from
Closed

Fix twig environment #593

wants to merge 3 commits into from

Conversation

mjauvin
Copy link
Member

@mjauvin mjauvin commented Jul 3, 2022

Fix #578

Credit to @RomainMazB

@mjauvin mjauvin added maintenance PRs that fix bugs, are translation changes or make only minor changes needs review Issues/PRs that require a review from a maintainer labels Jul 3, 2022
@mjauvin mjauvin added this to the v1.2.0 milestone Jul 3, 2022
@mjauvin mjauvin self-assigned this Jul 3, 2022
@mjauvin mjauvin requested a review from bennothommo July 3, 2022 23:14
@LukeTowers
Copy link
Member

Well it's somewhat hacky, but I don't have any major concerns with it. @bennothommo any thoughts?

@bennothommo
Copy link
Member

@mjauvin @RomainMazB is controller the only one needed? Would we need to support the other variables too, as per the Markup docs like this.page, this.layout, etc.?

@bennothommo bennothommo added needs response Issues/PRs where a maintainer is awaiting a response from the submitter and removed needs review Issues/PRs that require a review from a maintainer labels Jul 4, 2022
@RomainMazB
Copy link
Contributor

@bennothommo, all the needed variables is mentioned here in the code:

$this->vars['this'] = [
'page' => $this->page,
'layout' => $this->layout,
'theme' => $this->theme,
'param' => $this->router->getParameters(),
'controller' => $this,
'environment' => App::environment(),
'session' => App::make('session'),
];

Kind of surprised that add variables as global is an acceptable fix, sounds to me like a regression compared to the work made by @LukeTowers in PR #455 but I'm no the guy in charge here ;)

@mjauvin
Copy link
Member Author

mjauvin commented Jul 4, 2022

@bennothommo as far as I could see, controller is the only one needed:

# rgrep "'this'\]\['" modules

modules/cms/twig/Extension.php:        return $context['this']['controller']->renderPage();
modules/cms/twig/Extension.php:        return $context['this']['controller']->renderPartial($name, $parameters, $throwException);
modules/cms/twig/Extension.php:        return $context['this']['controller']->renderContent($name, $parameters);
modules/cms/twig/Extension.php:        return $context['this']['controller']->renderComponent($name, $parameters);
modules/cms/twig/Extension.php:        return $context['this']['controller']->makeAssets($type);
modules/cms/twig/Extension.php:     * @param array $context The Twig context for the call (relies on $context['this']['controller'] to exist)
modules/cms/twig/Extension.php:        return $context['this']['controller']->pageUrl($name, $parameters, $routePersistence);
modules/cms/twig/Extension.php:     * @param array $context The Twig context for the call (relies on $context['this']['controller'] to exist)
modules/cms/twig/Extension.php:        return $context['this']['controller']->themeUrl($url);

@RomainMazB
Copy link
Contributor

RomainMazB commented Jul 4, 2022

I discussed with @mjauvin on Discord about a related issue he met with the plugin: vojtasvoboda /
DynamicPDF.
He discovered that this issue is not only under the macro scope as the issue appeared within this plugin without the use of macro.

The plugin uses the registered twig.environment Facade from the system module to parse templates into an html page and then render it as a PDF file.

The plugin parses templates without being in any controller context, which results to the same "undefined this issue".

I may investigate furthermore on how to solve these issues without adding global variables, but as of today, re-introducing a global scope may be the best affordable solution.

For the needed variables: to me, even if I only use the controller variable through the |page filter, I guess that all the variable I mentioned earlier may be needed by someone else.
An example would be the access to a theme color config from a macro, or an url param using this.param.

[Edit] To confirm I just tested to use the theme in a macro, I confirm that it doesn't work with this basic test:

#theme.yaml
form:
  fields:
    main_color:
      label: Main color
      type: colorpicker
      default: "#FF0000"
{# Inside a macro #}
<span style="color: {{ this.theme.main_color }}">Some dummy text</span>

This doesn't work without adding the theme as a global variable.

@LukeTowers
Copy link
Member

Hmm, the global variables solution might not be enough then. With regards to the DynamicPDF plugin that's a separate issue, if someone wants to use the CMS twig environment to render a twig template then they need to refer to the correct namespace for it; we're not going to make the CMS features available in the system twig environment.

@RomainMazB What was the other solution that you proposed regarding overriding the processing of Macros in order to inject the correct context information into it? That sounds like that would be a better fix, especially when you consider a template itself might inject variables into the context that we might not have access to if we just go the global variable route.

@mjauvin
Copy link
Member Author

mjauvin commented Jul 4, 2022 via email

@RomainMazB
Copy link
Contributor

RomainMazB commented Jul 4, 2022

@LukeTowers, I just finished to work on my day project, I'll try to make a draft PR to see if what I suggested would work and if it could be more appropriate.

@LukeTowers
Copy link
Member

@mjauvin I don't see how it is, the System twig environment was never meant to have CMS features in it and before the changes I made in my PR to separate everything there were all sorts of issues that would crop up if you were to initialize it before doing things like sending emails for example.

Could you clarify what the BC break is?

@bennothommo
Copy link
Member

@mjauvin @RomainMazB would either of you be interested in making a unit test for both scenarios (the macro issue, and the issue from the Dynamic PDF plugin)? It would be worth it at least to ensure that any given solution we implement covers both scenarios.

Also, as for backwards compatibility - while it is one of our main goals, our major updates like with 1.2 does give us the leeway to make some backwards-incompatible changes if they are strongly justified. I am in agreement with Luke's rationale for the Twig changes in the first place, and reverting it back to fix these new issues is just going to bring back the issues that Luke's changes fixed.

@RomainMazB
Copy link
Contributor

I have a solution I made on my local machine, at least for the macro issue.

I was searching for any more convenient way before post it as a PR.

I'll post my code as a draft PR as soon as I am at home.

I didn't write tests for it atm.

@bennothommo
Copy link
Member

@RomainMazB @mjauvin I have confirmed that you will need the other variables as mentioned here defined in the globals if you want to support this.theme or this.page within a macro.

For example, using {{ this.page.fileName }} outside a macro will work fine, and uses the vars from the block of code that @RomainMazB mentioned here, but using that inside a macro will come up blank.

@LukeTowers
Copy link
Member

@bennothommo I'm hoping that @RomainMazB's suggestion of overriding the macro handling to include the correct context will pan out and we don't need to define a bunch of globals, especially since some of those don't exist when we would typically be defining the globals.

@bennothommo
Copy link
Member

I just tried to do that very thing. If Maz has worked that one out, he's a genius. 😜

@RomainMazB
Copy link
Contributor

I just tried to do that very thing. If Maz has worked that one out, he's a genius. 😜

Probably not a genius yet! I'm not fully convinced of the actual draft work: #598
It can be a starting point, at least it works.

LukeTowers added a commit that referenced this pull request Jul 10, 2022
…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.
@LukeTowers
Copy link
Member

Replaced by 5b8d189

@LukeTowers LukeTowers closed this Jul 10, 2022
@mjauvin mjauvin deleted the wip/fix-twig-context branch March 5, 2024 18:15
@mjauvin mjauvin removed this from the v1.2.0 milestone Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance PRs that fix bugs, are translation changes or make only minor changes needs response Issues/PRs where a maintainer is awaiting a response from the submitter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants