From 8809a1cab12202e9baccc26e9a6fea2da0a956a7 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 9 Jun 2010 08:20:51 +0200 Subject: [PATCH] changed as to = for the set tag and removed the need for as in short-notation of the block tag (closes #54) --- CHANGELOG | 2 +- doc/02-Twig-for-Template-Designers.markdown | 12 ++++++------ lib/Twig/Lexer.php | 2 +- lib/Twig/TokenParser/Block.php | 2 -- lib/Twig/TokenParser/Set.php | 2 +- test/fixtures/expressions/array.test | 2 +- test/fixtures/tags/block/basic.test | 2 +- test/fixtures/tags/for/inner_variables.test | 2 +- test/fixtures/tags/set/basic.test | 4 ++-- test/fixtures/tags/set/capture.test | 10 ++++++++++ test/fixtures/tags/set/expression.test | 2 +- 11 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/tags/set/capture.test diff --git a/CHANGELOG b/CHANGELOG index 045848b..f357805 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,7 @@ * 0.9.7-DEV Backward incompatibilities: - * added a 'as' string to the block tag short notation ({% block title "Title" %} must now be {% block title as "Title" %}) + * changed 'as' to '=' for the set tag ({% set title as "Title" %} must now be {% set title = "Title" %}) * removed the sandboxed attribute of the include tag (use the new sandbox tag instead) * refactored the Node system (if you have custom nodes, you will have to update them to use the new API) diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 34be297..79fe666 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -311,7 +311,7 @@ following constructs do the same: - [twig] - {% block title as page_title|title %} + {% block title page_title|title %} Import Context Behavior ----------------------- @@ -603,15 +603,15 @@ Inside code blocks you can also assign values to variables. Assignments use the `set` tag and can have multiple targets: [twig] - {% set foo as 'foo' %} + {% set foo = 'foo' %} - {% set foo as [1, 2] %} + {% set foo = [1, 2] %} - {% set foo as ['foo': 'bar] %} + {% set foo = ['foo': 'bar] %} - {% set foo as 'foo' ~ 'bar' %} + {% set foo = 'foo' ~ 'bar' %} - {% set foo, bar as 'foo', 'bar' %} + {% set foo, bar = 'foo', 'bar' %} The `set` tag can also be used to 'capture' chunks of HTML (new in Twig 0.9.6): diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index dc995a4..f1c304b 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -36,7 +36,7 @@ class Twig_Lexer implements Twig_LexerInterface const REGEX_NAME = '/[A-Za-z_][A-Za-z0-9_]*/A'; const REGEX_NUMBER = '/[0-9]+(?:\.[0-9]+)?/A'; const REGEX_STRING = '/(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/Asm'; - const REGEX_OPERATOR = '/<=? | >=? | [!=]= | \/\/ | \.\. | [(){}.,%*\/+~|-] | \[ | \] | \? | \:/Ax'; + const REGEX_OPERATOR = '/<=? | >=? | [!=]= | = | \/\/ | \.\. | [(){}.,%*\/+~|-] | \[ | \] | \? | \:/Ax'; public function __construct(Twig_Environment $env = null, array $options = array()) { diff --git a/lib/Twig/TokenParser/Block.php b/lib/Twig/TokenParser/Block.php index 1d2f846..ab80c3e 100644 --- a/lib/Twig/TokenParser/Block.php +++ b/lib/Twig/TokenParser/Block.php @@ -40,8 +40,6 @@ class Twig_TokenParser_Block extends Twig_TokenParser } } } else { - $stream->expect(Twig_Token::NAME_TYPE, 'as'); - $body = new Twig_Node(array( new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno), )); diff --git a/lib/Twig/TokenParser/Set.php b/lib/Twig/TokenParser/Set.php index 05a565d..25731c7 100644 --- a/lib/Twig/TokenParser/Set.php +++ b/lib/Twig/TokenParser/Set.php @@ -24,7 +24,7 @@ class Twig_TokenParser_Set extends Twig_TokenParser $names = $this->parser->getExpressionParser()->parseAssignmentExpression(); $capture = false; - if ($stream->test(Twig_Token::NAME_TYPE, 'as')) { + if ($stream->test(Twig_Token::OPERATOR_TYPE, '=')) { $stream->next(); list(, $values) = $this->parser->getExpressionParser()->parseMultitargetExpression(); diff --git a/test/fixtures/expressions/array.test b/test/fixtures/expressions/array.test index 3ebdeaa..32f9fe2 100644 --- a/test/fixtures/expressions/array.test +++ b/test/fixtures/expressions/array.test @@ -10,7 +10,7 @@ Twig supports array notation {{ [1, 'foo': 'bar']|keys|join(',') }} {# nested arrays #} -{% set a as [1, 2, [1, 2], 'foo': ['foo': 'bar']] %} +{% set a = [1, 2, [1, 2], 'foo': ['foo': 'bar']] %} {{ a[2]|join(',') }} {{ a["foo"]|join(',') }} diff --git a/test/fixtures/tags/block/basic.test b/test/fixtures/tags/block/basic.test index ecb7210..360dcf0 100644 --- a/test/fixtures/tags/block/basic.test +++ b/test/fixtures/tags/block/basic.test @@ -2,7 +2,7 @@ "block" tag --TEMPLATE-- {% block title1 %}FOO{% endblock %} -{% block title2 as foo|lower %} +{% block title2 foo|lower %} --TEMPLATE(foo.twig)-- {% block content %}{% endblock %} --DATA-- diff --git a/test/fixtures/tags/for/inner_variables.test b/test/fixtures/tags/for/inner_variables.test index 3e9ad56..49fb9ca 100644 --- a/test/fixtures/tags/for/inner_variables.test +++ b/test/fixtures/tags/for/inner_variables.test @@ -3,7 +3,7 @@ --TEMPLATE-- {% for i in 1..2 %} {% for j in 0..2 %} - {{k}}{% set k as k+1 %} {{ loop.parent.loop.index }} + {{k}}{% set k = k+1 %} {{ loop.parent.loop.index }} {% endfor %} {% endfor %} --DATA-- diff --git a/test/fixtures/tags/set/basic.test b/test/fixtures/tags/set/basic.test index 9c6fbd1..75ad6d3 100644 --- a/test/fixtures/tags/set/basic.test +++ b/test/fixtures/tags/set/basic.test @@ -1,11 +1,11 @@ --TEST-- "set" tag --TEMPLATE-- -{% set foo as 'foo' %} +{% set foo = 'foo' %} {{ foo }} -{% set foo, bar as 'foo', 'bar' %} +{% set foo, bar = 'foo', 'bar' %} {{ foo }}{{ bar }} --DATA-- diff --git a/test/fixtures/tags/set/capture.test b/test/fixtures/tags/set/capture.test new file mode 100644 index 0000000..4408ca8 --- /dev/null +++ b/test/fixtures/tags/set/capture.test @@ -0,0 +1,10 @@ +--TEST-- +"set" tag block capture +--TEMPLATE-- +{% set foo %}foo{% endset %} + +{{ foo }} +--DATA-- +return array() +--EXPECT-- +foo diff --git a/test/fixtures/tags/set/expression.test b/test/fixtures/tags/set/expression.test index 462d33c..8ff434a 100644 --- a/test/fixtures/tags/set/expression.test +++ b/test/fixtures/tags/set/expression.test @@ -1,7 +1,7 @@ --TEST-- "set" tag --TEMPLATE-- -{% set foo, bar as 'foo' ~ 'bar', 'bar' ~ 'foo' %} +{% set foo, bar = 'foo' ~ 'bar', 'bar' ~ 'foo' %} {{ foo }} {{ bar }} -- 1.7.2.5