Note that there are some explanatory texts on larger screens.

plurals
  1. PODefer execution/completion in Twig
    primarykey
    data
    text
    <p>I am using Twig separately from the Symfony2 framework, into something smaller rolled to suit my (perceived) needs better. Part of my framework is an asset management library that manages js/css dependencies, and after preprocessing, combining, minifying, gzipping, outputs the appropriate <code>&lt;link&gt;</code> and <code>&lt;script&gt;</code> tags to be placed in the <code>&lt;head&gt;</code> or in the bottom of <code>&lt;body&gt;</code>.</p> <p>The asset helper has only three public functions: <code>require_asset($asset)</code>, <code>render_head()</code> and <code>render_bottom()</code>. The first one is used sparingly throughout different template parts (I want to keep the loading of the assets outside the application logic, hence inside the twig templates). The two others need to be called after all assets have been required, and (after the asset manager does its thing) return the appropriate and tags to be placed in the template.</p> <p>All normal templates extend the base template:</p> <p>base.twig:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;{{ title }}&lt;/title&gt; &lt;!--[if lt IE 9]&gt;&lt;script src="//html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;&lt;![endif]--&gt; {{ Assets.headAssets }} &lt;/head&gt; &lt;/body&gt; {% block body %} {% endblock %} {{ Assets.bottomAssets }} &lt;body&gt; &lt;/html&gt; </code></pre> <p>here <code>{{ Assets.headAssets }}</code> and <code>{{ Assets.bottomAssets }}</code> are calls through previously declared Twig global variable <code>Assets</code> to the aforementioned <code>render_head()</code> and <code>render_bottom()</code> methods;</p> <p>In my instance, this base template is extended from test.twig:</p> <pre><code>{% extends "base.twig" %} {% import "forms.twig" as forms %} {% block body %} {{ Assets.requires('formtest.css') }} {{ Assets.requires('testscript.js') }} &lt;form method="post"&gt; {{ forms.form(form) }} &lt;input type="submit"/&gt; &lt;/form&gt; {% endblock %} </code></pre> <p>Here, <code>{{ Assets.requires('formtest.css') }}</code> and <code>{{ Assets.requires('testscript.js') }}</code> instruct the assets helper to include the css and js required by this template through calling <code>require_asset($asset)</code>.</p> <p>Normally, if done outside twig, the output functions would be called last, after all needed assets have been required. This works as expected.</p> <p>The problem I am running into has to do with the order of execution of these functions from the twig template. Since base.twig is called first, and contains the calls to the output functions, these get called before any requirement is made from the extending templates or included blocks/macros. Because of this the result is empty and does not take into account these new requirements.</p> <p>One way I have though of to solve this problem, would be to calculate the outputs after Twig has returned the rendering, and replace them in the returned result. This works, but i feel is not elegant enough.</p> <p>I was wondering if there was a way to postpone the execution of <code>{{ Assets.headAssets }}</code> and <code>{{ Assets.bottomAssets }}</code> until twig is done with everything else. Is there any way for me to achieve this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload