Code Monkey home page Code Monkey logo

Comments (8)

rybakit avatar rybakit commented on August 25, 2024

Could you provide a reproducer showing the erratic behavior (ideally, a minimal template, and the actual and expected output)?

from twig-deferred-extension.

mahagr avatar mahagr commented on August 25, 2024

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.

mahagr avatar mahagr commented on August 25, 2024

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.

rybakit avatar rybakit commented on August 25, 2024

@mahagr Could you please test #12?

from twig-deferred-extension.

mahagr avatar mahagr commented on August 25, 2024

Oh, that is a nice approach. I will test it when I have some extra time.

from twig-deferred-extension.

mahagr avatar mahagr commented on August 25, 2024

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.

rybakit avatar rybakit commented on August 25, 2024

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.

mahagr avatar mahagr commented on August 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.