Comments (8)
Could you provide a reproducer showing the erratic behavior (ideally, a minimal template, and the actual and expected output)?
from twig-deferred-extension.
I'll try to find some time. The template in question is a full HTML page with deferred CSS/JS loading in <head>
and the exception is thrown in the body. After the exception is thrown, I show a different page instead. JS renders before the HTML of the shown error page.
from twig-deferred-extension.
Here is the code to reproduce the issue:
#!/usr/bin/env php
<?php
use Twig\Environment;
use Twig\TwigFunction;
use Twig\Loader\ArrayLoader;
use Phive\Twig\Extensions\Deferred\DeferredExtension;
require __DIR__ . '/vendor/autoload.php';
$htmlTemplate = <<<HTML
{% block html %}
<html>
<head>
{% block head %}
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
{% endblock %}
{% block title %}
<title>Page Title</title>
{% endblock %}
{% block assets deferred %}
{{ style()|raw }}
{% endblock %}
</head>
<body>
{% block body %}
{% block content %}
{% endblock %}
{% endblock %}
</body>
</html>
{% endblock %}
HTML;
$errorTemplate = <<<ERROR
{% extends 'html' %}
{% block title %}
<title>Error</title>
{% endblock %}
{% block content %}
<h1>Error</h1>
{% endblock %}
ERROR;
$pageTemplate = <<<CONTENT
{% extends 'html' %}
{% block title %}
<title>Page Title</title>
{% endblock %}
{% block content %}
<h1>Page content</h1>
<p>
Content {{ error() }}
</p>
{% endblock %}
CONTENT;
$loader = new ArrayLoader(['html' => $htmlTemplate, 'error' => $errorTemplate, 'page' => $pageTemplate]);
$twig = new Environment($loader, []);
$twig->addExtension(new DeferredExtension());
$twig->addFunction(
new TwigFunction('error', function () { throw new \Exception('Error!'); })
);
$twig->addFunction(
new TwigFunction('style', function () { return '<style>head { text-color: red; }</style>'; })
);
$context = [];
try {
$html = $twig->render('page', $context);
} catch (\Exception $e) {
$html = $twig->render('error', $context);
}
echo $html;
If you run the code, it will render contents of style()
before the document starts.
from twig-deferred-extension.
@mahagr Could you please test #12?
from twig-deferred-extension.
Oh, that is a nice approach. I will test it when I have some extra time.
from twig-deferred-extension.
BTW, your extension works as it is in the latest Twig 2.14 as well. I would add it to the supported list as long as you require also "php": ">=7.2.5"
.
Also Twig 1.44 support is easy to add, you only need this:
https://github.com/getgrav/grav/blob/develop/system/src/Twig/DeferredExtension/DeferredNodeVisitorCompat.php
https://github.com/getgrav/grav/blob/develop/system/src/Twig/DeferredExtension/DeferredExtension.php#L31-L34
from twig-deferred-extension.
BTW, your extension works as it is in the latest Twig 2.14 as well. I would add it to the supported list as long as you require also "php": ">=7.2.5".
This will require updating CI to ensure that the tests pass on the lowest supported version of twig (2.14 in this case) as well as on the current 3.x. I'm not sure I have the time and interest to do that, but I might consider accepting a pull request if you're interested in working on it.
Also Twig 1.44 support is easy to add
If people are using such an ancient version, they probably aren't interested in any updates, fixes, or new features. In this case, they can continue using v1 of the extension.
from twig-deferred-extension.
Some of us are stuck with the old version due to backward compatibility. :(
Anyways, I have the fix for those if they click the links above.
from twig-deferred-extension.
Related Issues (13)
- {% block foo deferred %} HOT 1
- Wonderful library HOT 1
- bug: undefined variable $macros
- Redefining a deferred block that is not the first removes other blocks from rendering HOT 2
- Add tests
- Deferred blocks broken with twig/twig 3.9.0/3.9.1 HOT 3
- The compiled methods can lead to invalid PHP code due to duplicate method names
- Deferred blocks are breaking the renderBlock API of Twig HOT 4
- Does not work with version ^1.18 HOT 3
- Setting data not working when we include a template from string HOT 2
- Update namespace and rename classes
- Twig 3 support HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twig-deferred-extension.