2017 © Pedro Peláez
 

library twig-extensions-deferred

An extension for Twig that allows to defer block rendering

image

phive/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  • Wednesday, March 14, 2018
  • by rybakit
  • Repository
  • 7 Watchers
  • 79 Stars
  • 106,275 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 11 Versions
  • 15 % Grown

The README.md

Twig Deferred Extension

Quality Assurance Scrutinizer Code Quality Mentioned in Awesome Twig, (*1)

An extension for Twig that allows to defer block rendering., (*2)

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., (*3)

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"., (*4)

Advanced example

Just for example purposes, first create a global twig variable:, (*5)

use Twig\Environment;

...

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

Then build the following set of templates:, (*6)

{# 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:, (*7)

<!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., (*8)

The Versions

14/03 2018

dev-master

9999999-dev https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

14/03 2018

v2.0.1

2.0.1.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

19/03 2017

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

17/03 2017

v2.0.0

2.0.0.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

17/03 2017

v1.0.2

1.0.2.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

17/03 2017

v1.0.1

1.0.1.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

12/01 2017

v1.0.0

1.0.0.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

05/04 2015

v0.3.0

0.3.0.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

01/01 2015

v0.2.0

0.2.0.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

30/05 2014

v0.1.0

0.1.0.0 https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer

28/05 2014

dev-block_reference

dev-block_reference https://github.com/rybakit/twig-extensions-deferred

An extension for Twig that allows to defer block rendering

  Sources   Download

MIT

The Requires

 

by Eugene Leonovich

twig extension lazy defer