Horizontal Reuse
----------------
+.. versionadded:: 1.1
+ Horizontal reuse was added in Twig 1.1.
+
.. note::
Horizontal reuse is an advanced Twig feature that is hardly ever needed in
{% block sidebar %}{% endblock %}
In this example, the ``use`` statement imports the ``sidebar`` block into the
-main template. The code is exactly equivalent to the following one:
+main template. The code is mostly equivalent to the following one (the
+imported blocks are not outputted automatically):
.. code-block:: jinja
->indent()
;
- for ($i = count($this->getNode('traits')) - 1; $i >= 0; --$i) {
+ for ($i = 0, $count = count($this->getNode('traits')); $i < $count; $i++) {
$compiler
->write(sprintf("\$_trait_%s_blocks,\n", $i))
;
--- /dev/null
+--TEST--
+"use" tag
+--TEMPLATE--
+{% use "blocks.twig" with content as foo %}
+
+{{ block('foo') }}
+--TEMPLATE(blocks.twig)--
+{% block content 'foo' %}
+--DATA--
+return array()
+--EXPECT--
+foo
--- /dev/null
+--TEST--
+"use" tag
+--TEMPLATE--
+{% use "blocks.twig" %}
+
+{{ block('content') }}
+--TEMPLATE(blocks.twig)--
+{% block content 'foo' %}
+--DATA--
+return array()
+--EXPECT--
+foo
--- /dev/null
+--TEST--
+"use" tag
+--TEMPLATE--
+{% use "foo.twig" %}
+
+{{ block('content') }}
+{{ block('foo') }}
+{{ block('bar') }}
+--TEMPLATE(foo.twig)--
+{% use "bar.twig" %}
+
+{% block content 'foo' %}
+{% block foo 'foo' %}
+--TEMPLATE(bar.twig)--
+{% block content 'bar' %}
+{% block bar 'bar' %}
+--DATA--
+return array()
+--EXPECT--
+foo
+foo
+bar
--- /dev/null
+--TEST--
+"use" tag
+--TEMPLATE--
+{% use "foo.twig" %}
+{% use "bar.twig" %}
+
+{{ block('content') }}
+{{ block('foo') }}
+{{ block('bar') }}
+--TEMPLATE(foo.twig)--
+{% block content 'foo' %}
+{% block foo 'foo' %}
+--TEMPLATE(bar.twig)--
+{% block content 'bar' %}
+{% block bar 'bar' %}
+--DATA--
+return array()
+--EXPECT--
+bar
+foo
+bar
--- /dev/null
+--TEST--
+"use" tag
+--TEMPLATE--
+{% use "foo.twig" with content as foo_content %}
+{% use "bar.twig" %}
+
+{{ block('content') }}
+{{ block('foo') }}
+{{ block('bar') }}
+{{ block('foo_content') }}
+--TEMPLATE(foo.twig)--
+{% block content 'foo' %}
+{% block foo 'foo' %}
+--TEMPLATE(bar.twig)--
+{% block content 'bar' %}
+{% block bar 'bar' %}
+--DATA--
+return array()
+--EXPECT--
+bar
+foo
+bar
+foo