Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
231 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
``apply`` | ||
========= | ||
|
||
The ``apply`` tag allows you to apply Twig filters on a block of template data: | ||
|
||
.. code-block:: jinja | ||
{% apply upper %} | ||
This text becomes uppercase | ||
{% endapply %} | ||
You can also chain filters and pass arguments to them: | ||
|
||
.. code-block:: jinja | ||
{% apply lower|escape('html') %} | ||
<strong>SOME TEXT</strong> | ||
{% endapply %} | ||
{# outputs "<strong>some text</strong>" #} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Twig\TokenParser; | ||
|
||
use Twig\Node\Expression\TempNameExpression; | ||
use Twig\Node\Node; | ||
use Twig\Node\PrintNode; | ||
use Twig\Node\SetNode; | ||
use Twig\Token; | ||
|
||
/** | ||
* Applies filters on a section of a template. | ||
* | ||
* {% apply upper %} | ||
* This text becomes uppercase | ||
* {% endapplys %} | ||
*/ | ||
final class ApplyTokenParser extends AbstractTokenParser | ||
{ | ||
public function parse(Token $token) | ||
{ | ||
$lineno = $token->getLine(); | ||
$name = $this->parser->getVarName(); | ||
|
||
$ref = new TempNameExpression($name, $lineno); | ||
$ref->setAttribute('always_defined', true); | ||
|
||
$filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag()); | ||
|
||
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE); | ||
$body = $this->parser->subparse([$this, 'decideApplyEnd'], true); | ||
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE); | ||
|
||
return new Node([ | ||
new SetNode(true, $ref, $body, $lineno, $this->getTag()), | ||
new PrintNode($filter, $lineno, $this->getTag()), | ||
]); | ||
} | ||
|
||
public function decideApplyEnd(Token $token) | ||
{ | ||
return $token->test('endapply'); | ||
} | ||
|
||
public function getTag() | ||
{ | ||
return 'apply'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
"apply" tag applies a filter on its children | ||
--TEMPLATE-- | ||
{% apply upper %} | ||
Some text with a {{ var }} | ||
{% endapply %} | ||
--DATA-- | ||
return ['var' => 'var'] | ||
--EXPECT-- | ||
SOME TEXT WITH A VAR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--TEST-- | ||
"apply" tag applies a filter on its children | ||
--TEMPLATE-- | ||
{% apply json_encode|raw %}test{% endapply %} | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
"test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
"apply" tags accept multiple chained filters | ||
--TEMPLATE-- | ||
{% apply lower|title %} | ||
{{ var }} | ||
{% endapply %} | ||
--DATA-- | ||
return ['var' => 'VAR'] | ||
--EXPECT-- | ||
Var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
"apply" tags can be nested at will | ||
--TEMPLATE-- | ||
{% apply lower|title %} | ||
{{ var }} | ||
{% apply upper %} | ||
{{ var }} | ||
{% endapply %} | ||
{{ var }} | ||
{% endapply %} | ||
--DATA-- | ||
return ['var' => 'var'] | ||
--EXPECT-- | ||
Var | ||
Var | ||
Var |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
"apply" tag does not create a new scope | ||
--TEMPLATE-- | ||
{% set foo = 'baz' %} | ||
{% apply spaceless %} | ||
{% set foo = 'foo' %} | ||
{% set bar = 'bar' %} | ||
{% endapply %} | ||
{{ 'foo' == foo ? 'OK ' ~ foo : 'KO' }} | ||
{{ 'bar' == bar ? 'OK ' ~ bar : 'KO' }} | ||
--DATA-- | ||
return [] | ||
--EXPECT-- | ||
OK foo | ||
OK bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
"apply" tag applies the filter on "for" tags | ||
--TEMPLATE-- | ||
{% apply upper %} | ||
{% for item in items %} | ||
{{ item }} | ||
{% endfor %} | ||
{% endapply %} | ||
--DATA-- | ||
return ['items' => ['a', 'b']] | ||
--EXPECT-- | ||
A | ||
B |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--TEST-- | ||
"apply" tag applies the filter on "if" tags | ||
--TEMPLATE-- | ||
{% apply upper %} | ||
{% if items %} | ||
{{ items|join(', ') }} | ||
{% endif %} | ||
|
||
{% if items.3 is defined %} | ||
FOO | ||
{% else %} | ||
{{ items.1 }} | ||
{% endif %} | ||
|
||
{% if items.3 is defined %} | ||
FOO | ||
{% elseif items.1 %} | ||
{{ items.0 }} | ||
{% endif %} | ||
|
||
{% endapply %} | ||
--DATA-- | ||
return ['items' => ['a', 'b']] | ||
--EXPECT-- | ||
A, B | ||
|
||
B | ||
|
||
A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
"scope" tag creates a new scope | ||
--TEMPLATE-- | ||
{% filter spaceless %} | ||
{% set item = 'foo' %} | ||
{% endfilter %} | ||
{{ item }} | ||
--DATA-- | ||
return [] | ||
--EXCEPTION-- | ||
Twig\Error\RuntimeError: Variable "item" does not exist in "index.twig" at line 5. |