From 3930ba0f4e21fa38e060093728626fe439e8267d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 29 Apr 2011 09:42:28 +0200 Subject: [PATCH] added some tests for the use tag --- doc/templates.rst | 6 ++++- lib/Twig/Node/Module.php | 2 +- test/Twig/Tests/Fixtures/tags/use/aliases.test | 12 ++++++++++ test/Twig/Tests/Fixtures/tags/use/basic.test | 12 ++++++++++ test/Twig/Tests/Fixtures/tags/use/deep.test | 22 +++++++++++++++++++ test/Twig/Tests/Fixtures/tags/use/multiple.test | 21 ++++++++++++++++++ .../Tests/Fixtures/tags/use/multiple_aliases.test | 23 ++++++++++++++++++++ 7 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/use/aliases.test create mode 100644 test/Twig/Tests/Fixtures/tags/use/basic.test create mode 100644 test/Twig/Tests/Fixtures/tags/use/deep.test create mode 100644 test/Twig/Tests/Fixtures/tags/use/multiple.test create mode 100644 test/Twig/Tests/Fixtures/tags/use/multiple_aliases.test diff --git a/doc/templates.rst b/doc/templates.rst index fd618f7..6891b1c 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -1470,6 +1470,9 @@ http://github.com/fabpot/Twig-extensions. 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 @@ -1509,7 +1512,8 @@ The ``use`` statement tells Twig to import the blocks defined 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 diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 7776869..83bee45 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -163,7 +163,7 @@ class Twig_Node_Module extends Twig_Node ->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)) ; diff --git a/test/Twig/Tests/Fixtures/tags/use/aliases.test b/test/Twig/Tests/Fixtures/tags/use/aliases.test new file mode 100644 index 0000000..f887006 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/use/aliases.test @@ -0,0 +1,12 @@ +--TEST-- +"use" tag +--TEMPLATE-- +{% use "blocks.twig" with content as foo %} + +{{ block('foo') }} +--TEMPLATE(blocks.twig)-- +{% block content 'foo' %} +--DATA-- +return array() +--EXPECT-- +foo diff --git a/test/Twig/Tests/Fixtures/tags/use/basic.test b/test/Twig/Tests/Fixtures/tags/use/basic.test new file mode 100644 index 0000000..7364d76 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/use/basic.test @@ -0,0 +1,12 @@ +--TEST-- +"use" tag +--TEMPLATE-- +{% use "blocks.twig" %} + +{{ block('content') }} +--TEMPLATE(blocks.twig)-- +{% block content 'foo' %} +--DATA-- +return array() +--EXPECT-- +foo diff --git a/test/Twig/Tests/Fixtures/tags/use/deep.test b/test/Twig/Tests/Fixtures/tags/use/deep.test new file mode 100644 index 0000000..b551a1e --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/use/deep.test @@ -0,0 +1,22 @@ +--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 diff --git a/test/Twig/Tests/Fixtures/tags/use/multiple.test b/test/Twig/Tests/Fixtures/tags/use/multiple.test new file mode 100644 index 0000000..198be0c --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/use/multiple.test @@ -0,0 +1,21 @@ +--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 diff --git a/test/Twig/Tests/Fixtures/tags/use/multiple_aliases.test b/test/Twig/Tests/Fixtures/tags/use/multiple_aliases.test new file mode 100644 index 0000000..8de871a --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/use/multiple_aliases.test @@ -0,0 +1,23 @@ +--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 -- 1.7.2.5