added with tag

This commit is contained in:
Fabien Potencier
2016-11-12 12:46:34 -08:00
parent c19448545a
commit 4366907d73
10 changed files with 212 additions and 0 deletions
+1
View File
@@ -22,3 +22,4 @@ Tags
spaceless
use
verbatim
with
+42
View File
@@ -0,0 +1,42 @@
``with``
========
.. versionadded:: 1.28
The ``with`` tag was added in Twig 1.28.
Use the ``with`` tag to create a new inner scope. Variables set within this
scope are not visible outside of the scope:
.. code-block:: jinja
{% with %}
{% set foo = 42 %}
{{ foo }} foo is 42 here
{% endwith %}
foo is not visible here any longer
Instead of defining variables at the beginning of the scope, you can pass a
hash of variables you want to define in the ``with`` tag; the previous example
is equivalent to the following one:
.. code-block:: jinja
{% with { foo: 42 } %}
{{ foo }} foo is 42 here
{% endwith %}
foo is not visible here any longer
{# it works with any expression that resolves to a hash #}
{% set vars = { foo: 42 } %}
{% with vars %}
...
{% endwith %}
By default, the inner scope has access to the outer scope context; you can
disable this behavior by appending the ``only`` keyword:
{% set bar = 'bar' %}
{% with { foo: 42 } only %}
{# only foo is defined #}
{# bar is not defined #}
{% endwith %}
+1
View File
@@ -132,6 +132,7 @@ class Twig_Extension_Core extends Twig_Extension
new Twig_TokenParser_Flush(),
new Twig_TokenParser_Do(),
new Twig_TokenParser_Embed(),
new Twig_TokenParser_With(),
);
}
+62
View File
@@ -0,0 +1,62 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2016 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents a nested "with" scope.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_With extends Twig_Node
{
public function __construct(Twig_Node $body, Twig_Node $variables = null, $only = false, $lineno, $tag = null)
{
$nodes = array('body' => $body);
if (null !== $variables) {
$nodes['variables'] = $variables;
}
parent::__construct($nodes, array('only' => (bool) $only), $lineno, $tag);
}
public function compile(Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
if ($this->hasNode('variables')) {
$varsName = $compiler->getVarName();
$compiler
->write(sprintf('$%s = ', $varsName))
->subcompile($this->getNode('variables'))
->raw(";\n")
->write(sprintf("if (!is_array(\$%s)) {\n", $varsName))
->indent()
->write("throw new Twig_Error_Runtime('Variables passed to the \"with\" tag must be a hash.');\n")
->outdent()
->write("}\n")
;
if ($this->getAttribute('only')) {
$compiler->write("\$context = array('_parent' => \$context);\n");
} else {
$compiler->write("\$context['_parent'] = \$context;\n");
}
$compiler->write(sprintf("\$context = array_merge(\$context, \$%s);\n", $varsName));
} else {
$compiler->write("\$context['_parent'] = \$context;\n");
}
$compiler
->subcompile($this->getNode('body'))
->write("\$context = \$context['_parent'];\n")
;
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2016 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Creates a nested scope.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_TokenParser_With extends Twig_TokenParser
{
public function parse(Twig_Token $token)
{
$stream = $this->parser->getStream();
$variables = null;
$only = false;
if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
$variables = $this->parser->getExpressionParser()->parseExpression();
$only = $stream->nextIf(Twig_Token::NAME_TYPE, 'only');
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideWithEnd'), true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
return new Twig_Node_With($body, $variables, $only, $token->getLine(), $this->getTag());
}
public function decideWithEnd(Twig_Token $token)
{
return $token->test('endwith');
}
public function getTag()
{
return 'with';
}
}
@@ -0,0 +1,13 @@
--TEST--
"with" tag
--TEMPLATE--
{% with %}
{% set bar = 'BAZ' %}
{{ foo }}{{ bar }}
{% endwith %}
{{ foo }}{{ bar }}
--DATA--
return array('foo' => 'foo', 'bar' => 'bar')
--EXPECT--
fooBAZ
foobar
@@ -0,0 +1,10 @@
--TEST--
"with" tag with expression
--TEMPLATE--
{% with {foo: 'foo', bar: 'BAZ'} %}
{{ foo }}{{ bar }}
{% endwith %}
--DATA--
return array('foo' => 'baz')
--EXPECT--
fooBAZ
@@ -0,0 +1,15 @@
--TEST--
nested "with" tags
--TEMPLATE--
{% set foo, bar = 'foo', 'bar' %}
{% with {bar: 'BAZ'} %}
{% with {foo: 'FOO'} %}
{{ foo }}{{ bar }}
{% endwith %}
{% endwith %}
{{ foo }}{{ bar }}
--DATA--
return array()
--EXPECT--
FOOBAZ
foobar
@@ -0,0 +1,10 @@
--TEST--
"with" tag with an expression that is not a hash
--TEMPLATE--
{% with vars %}
{{ foo }}{{ bar }}
{% endwith %}
--DATA--
return array('vars' => 'no-hash')
--EXCEPTION--
Twig_Error_Runtime: Variables passed to the "with" tag must be a hash in "index.twig" at line 2.
@@ -0,0 +1,10 @@
--TEST--
"with" tag with expression and only
--TEMPLATE--
{% with {foo: 'foo', bar: 'BAZ'} only %}
{{ foo }}{{ bar }}{{ baz }}
{% endwith %}
--DATA--
return array('foo' => 'baz', 'baz' => 'baz')
--EXCEPTION--
Twig_Error_Runtime: Variable "baz" does not exist in "index.twig" at line 3.