-
-
Notifications
You must be signed in to change notification settings - Fork 61
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 twig 3.9 #154
Support for twig 3.9 #154
Conversation
This might mitigate the problem and should be released as As an additional upgrading notice we could add an information that you have to use something like |
This patch broke one test case so I still have to look at that. After that I can send another pr for the 2.5 release |
As far as I can tell your patch did not break things. It's rather another side-effect of twig 3.9, but I have no idea what exactly, the test does not find a script tag from the asset bundle. See https://github.com/schmunk42/yii2-twig/actions/runs/8799762201 - which just has this additional commit applied schmunk42@f2bb14b (prevent installation of twig 3.9 and above) - test are passing. I'd suggest to add this to the PR and take care about the actual problem later. |
Yes, I have looked at it, but did not yet found the source of the problem. It seems that the yii2 view helpers(end_body, start_body etc...) twig extensions are broken in twig3.9 for some reason. see commit twigphp/Twig@3b6cbf98d I agree, that first there should be a conflicting composer patch, so no problematic updates happens. |
This pr already contains a partial fix for 3.9. Do you think there should be another pr for just the conflict or use this and open another with the later actual fix? |
see also #154 (comment) and following
See #155 |
It seems that yii2 view helper functions like 'begin_body' 'begin_page' etc. will mark the parts of the page with '' comments and in the last 'end_page' view method call will read 'ob_get_clean()' and replace these blocks with the tags that needs to be placed there. (in case of the failing test it is 'jquery.js' file. For some reason this does not happen with twig 3.9. |
That's the same thing I found. At some point the output buffer is emptied where it shouldn't be. At least that would be my first guess. |
I think the ob clear happens here. https://github.com/twigphp/Twig/blob/3.x/src/Template.php#L362 |
Yup, looks like that‘s the spot |
It seems that they now only capture and clear whats in the ob but not echoing anymore, so it seems to me that we are unable to change the html in the buffer. |
I think this issue is related to our problem. twigphp/Twig#4034 |
See also #154 (comment) and following
If I'm interpreting the code correctly, the twig guys are turning everything completely inside out, at least when it comes to the compilation of twig. An error is explicitly triggered here if “echo” is used:
I don't see an easy way to get this running with twig version 3.9.0 and above without having to touch the view component. |
If I understood correctly, they are preparing the code for v4 when it will only use the new yield with no ob buffer. Maybe if we could somehow access and modify the twig content from the TwigFunction, then we could use ob inside the viewHelper to capture the yii helper's echo, in case of end_body the ob would need to have the twig content so the function can replace the custom placeholders. Or create a custom View as u said, but I think that would be a bit inconvenient and breaking change. |
I'm currently trying out exactly what you're suggesting. and as you say, I wouldn't see a custom view component as an option either |
That is my current approach. I try to use the existing public function viewHelper($context, $name = null)
{
if ($name !== null && isset($context['this'])) {
if ($name == 'end_page') {
// tapping into the after render event of the view component to load the rendered content into the output buffer
Event::on($context['this']::class, View::EVENT_AFTER_RENDER, function ($event) {
// TODO: Is loading stuff in output buffer the way?
// $output = $event->output;
});
}
$this->call($context['this'], Inflector::variablize($name));
}
} |
Were you able to get it working? I can think of another solution(to work without yield and use the current yii views) yii2-twig/src/ViewRenderer.php Line 179 in 0ed174a
I think this can work, but I can only test it tomorrow. |
Just to sum up the problem that is braking the test (as I understand it) So this package was modifiing twig's global variable(the ob), which it has no access to modify(only append using echo) since twig:3.9. |
This seems to be working correctly! I will push the changes soon. |
@schmunk42 @eluhr I think this workaround should work with twig 3.9 version as well! Could you please check it out? I think the best solution would be to support use_yield as it will be the only option in twig 4.0, but still I do not see any good solution for it without a breaking change in this library. ps: This relies on the fact that the only yii2 View method that modifies the output buffer is the endPage. I've checked and it seems to be true, but this needs another check for sure update: if you dont think we need to support older twig versions, I could remove the version checks and update composer.json to conflict with older versions. That is up to you, which would be the better choice for the project :) |
src/TwigVersionHelper.php
Outdated
*/ | ||
public static function above39(): bool | ||
{ | ||
return !function_exists('twig_get_attribute'); |
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.
I do not think this is the best way to check the version. Do anyone have a better idea?
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.
Can't think of a better way to check this and since the non-existence of the function is the source of our current problems, I think this is actually a pretty good solution.
@diffy0712 with your current patch i am getting the following error: if ($content !== false) {
ob_clean();
} But even after that fix i get an What is your way for recreating the bug? I tested it by using using the layout.twig from the tests as my web application layout 'layout' => '@vendor/yiisoft/yii2-twig/tests/views/layout.twig' |
@yiisoft/core-developers Given these esoteric issues, I'd even think about releasing a new major version of Because twig can be configured with several plugins, custom functions, probably usage of "protected" functions in your templates and so on. It looks highly likely to me that upgrading to |
I've done the fix by running the unit tests. It seems that I have to check out in my project as well, because it seems that there is some problems with the buffer part of the code. |
Yes that is true, the same issue that the end_page twig function failed with twig:3.9 it can happen in anyone's project using yii2-twig. In this case I would remove the old version support from this pr and update the compose.json as well. |
@diffy0712 Yes please, I think we should make a hard cut. @samdark I don't want to be annoying, but is there something preventing you from creating the |
@eluhr I've fixed the render output buffer issue. Could you please check if it is working for you as well? |
@schmunk42 I've removed the support for twig below 3.9 and added the updated composer version dependency for twig to 3.9. |
I still need to add description to the Changelog for the new release to explain this change and the situation with > twig 3.9 better. I will try to do it tomorrow. |
@schmunk42 not really. Just very busy. |
@diffy0712 LGTM! |
I also ran our phd5 tests against this patch - all green 👍 So I'd revise my statement about a new major version :) |
Great news, thank you. I think the twig internal change that might affect others the most is the fact that it now does not store the contents in the output buffer. So if others relied on this and modified the output buffer in twig function calls (like yii2-twig did with view->endPage twigFunction) it will break with twig 3.9. Twig with 'use_yield=false' will still read the output buffer as expected in twigFunction calls. I try to update the changelog today. |
@schmunk42 so do we need to release from master before merging this PR? |
Yes, I’d recommend to release current master as 2.4.3
And the PR as 2.5.0 or 3.0.0
|
I've updated the changelog as well. I think this pr is ready for review. |
@schmunk42 how to continue with this? |
I think it should be merged and released as |
@schmunk42 done. Thanks, @diffy0712 |
@samdark Don't forget to release ;) https://github.com/yiisoft/yii2-twig/tags |
Twig 3.9 introduced a few internal changes that this package relied on:
twig_get_attribute
was moved and renamed toCoreExtension::getAttribute
.See discussion about moving functions to internal in twig:3.9 here.