All Projects → rybakit → Twig Deferred Extension

rybakit / Twig Deferred Extension

Licence: mit
An extension for Twig that allows to defer block rendering.

Projects that are alternatives of or similar to Twig Deferred Extension

Fmbbcodebundle
🔠 BBCode bundle for Symfony projects
Stars: ✭ 56 (-42.27%)
Mutual labels:  twig, twig-extension
Cssinliner Extra
CSS Inliner support for Twig
Stars: ✭ 93 (-4.12%)
Mutual labels:  twig, twig-extension
cache-extra
Template fragment cache support for Twig
Stars: ✭ 24 (-75.26%)
Mutual labels:  twig, twig-extension
Inky Extension
[DEPRECATED] Inky email template engine support for Twig
Stars: ✭ 57 (-41.24%)
Mutual labels:  twig, twig-extension
Html Extra
HTML extension for Twig
Stars: ✭ 24 (-75.26%)
Mutual labels:  twig, twig-extension
PhpStorm-Live-Templates-Craft-CMS
PhpStorm Live Templates for Craft CMS
Stars: ✭ 34 (-64.95%)
Mutual labels:  twig, twig-extension
oc-twigextensions-plugin
Twig extensions plugin for OctoberCMS
Stars: ✭ 17 (-82.47%)
Mutual labels:  twig, twig-extension
breadcrumb-bundle
Symfony bundle for easy breadcrumbs management
Stars: ✭ 26 (-73.2%)
Mutual labels:  twig, twig-extension
Twig Cache Extension
Stars: ✭ 67 (-30.93%)
Mutual labels:  twig, twig-extension
Twig Extra Bundle
The Twig bundle for official extra extensions
Stars: ✭ 389 (+301.03%)
Mutual labels:  twig, twig-extension
markdown-extra
Markdown extension for Twig
Stars: ✭ 83 (-14.43%)
Mutual labels:  twig, twig-extension
Awesome Twig
A curated list of amazingly awesome Twig extensions, snippets and tutorials
Stars: ✭ 63 (-35.05%)
Mutual labels:  twig, twig-extension
DrupalTwigFood
Useful functions, filters for twig @ Drupal 8
Stars: ✭ 1 (-98.97%)
Mutual labels:  twig, twig-extension
twig-translation
A Twig Translation Extension
Stars: ✭ 15 (-84.54%)
Mutual labels:  twig, twig-extension
cssinliner-extension
[DEPRECATED] CSS inliner support for Twig
Stars: ✭ 30 (-69.07%)
Mutual labels:  twig, twig-extension
joomla-twig
Twig 2.0 & Twig extensions integration for Joomla! https://phproberto.github.io/joomla-twig/
Stars: ✭ 25 (-74.23%)
Mutual labels:  twig, twig-extension
Reactrenderer
Client and Server-side React rendering from PHP
Stars: ✭ 201 (+107.22%)
Mutual labels:  twig, twig-extension
Slugify
Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.
Stars: ✭ 2,697 (+2680.41%)
Mutual labels:  twig, twig-extension
time-ago-bundle
Provides a simple twig filter for expressing time difference in words.
Stars: ✭ 13 (-86.6%)
Mutual labels:  twig, twig-extension
Html Compress Twig
Twig extension for compressing HTML and inline CSS/JS using WyriHaximus/HtmlCompress
Stars: ✭ 72 (-25.77%)
Mutual labels:  twig, twig-extension

Twig Deferred Extension

Quality Assurance Scrutinizer Code Quality Mentioned in Awesome Twig

An extension for Twig that allows to defer block rendering.

Installation

composer require rybakit/twig-deferred-extension

Note that this extension requires Twig 3 or above. If you need support for older versions of Twig, please refer to the legacy repository.

Initialization

use Twig\DeferredExtension\DeferredExtension;
use Twig\Environment;

...

$twig = new Environment($loader);
$twig->addExtension(new DeferredExtension());

Simple example

{% block foo deferred %}
    {{ bar }}
{% endblock %}

{% set bar = 'bar' %}

The foo block will output "bar".

Advanced example

Just for example purposes, first create a global twig variable:

use Twig\Environment;

...

$twig = new Environment($loader);
$twig->addGlobal('assets', new \ArrayObject());

Then build the following set of templates:

{# layout.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        {% block content '' %}

        {{ assets.append('/js/layout-header.js') }}

        {% block javascripts deferred %}
            {% for asset in assets %}
                <script src="{{ asset }}"></script>
            {% endfor %}
        {% endblock %}

        {{ assets.append('/js/layout-footer.js') }}
    </body>
</html>


{# page.html.twig #}
{% extends "layout.html.twig" %}

{% block content %}
    {{ assets.append('/js/page-header.js') }}

    {% if foo is defined %}
        {{ include("subpage1.html.twig") }}
    {% else %}
        {{ include("subpage2.html.twig") }}
    {% endif %}

    {{ assets.append('/js/page-footer.js') }}
{% endblock %}


{# subpage1.html.twig #}
{{ assets.append('/js/subpage1.js') }}


{# subpage2.html.twig #}
{{ assets.append('/js/subpage2.js') }}

The resulting html will be the following:

<!DOCTYPE html>
<html>
    <head>
        ...
    </head>
    <body>
        <script src="/js/layout-header.js"></script>
        <script src="/js/page-header.js"></script>
        <script src="/js/subpage2.js"></script>
        <script src="/js/page-footer.js"></script>
        <script src="/js/layout-footer.js"></script>
    </body>
</html>

Block overriding

{# index.twig #}
{% extends "base.twig" %}
{% block foo %}foo is not deferred anymore{% endblock %}
{% block bar deferred %}bar is deferred now{% endblock %}

{# base.twig #}
{% block foo deferred %}foo is deferred{% endblock %}
{% block bar %}bar is not deferred{% endblock %}

License

The library is released under the MIT License. See the bundled LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].