mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-12 02:16:41 +00:00
added 'is defined' support for block()
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 1.28.0 (2016-XX-XX)
|
||||
|
||||
* added "is defined" support for block()
|
||||
* optimized the way attributes are fetched
|
||||
|
||||
* 1.27.0 (2016-10-25)
|
||||
|
||||
@@ -12,4 +12,13 @@ times, use the ``block`` function:
|
||||
|
||||
{% block body %}{% endblock %}
|
||||
|
||||
Use the ``defined`` test to check if a block exists in the context of the
|
||||
current template:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% if block("footer") is defined %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
.. seealso:: :doc:`extends<../tags/extends>`, :doc:`parent<../functions/parent>`
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
``block name``
|
||||
==============
|
||||
|
||||
.. versionadded:: 1.28
|
||||
The ``block name`` test was added in Twig 1.28.
|
||||
|
||||
``block name`` checks if a block is defined in the current context of the template.
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% if 'title' is block name %}
|
||||
<title>{{ block('title') }}<title>
|
||||
{% endif %}
|
||||
@@ -4,7 +4,6 @@ Tests
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
block_name
|
||||
constant
|
||||
defined
|
||||
divisibleby
|
||||
|
||||
@@ -222,7 +222,6 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
new Twig_SimpleTest('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')),
|
||||
new Twig_SimpleTest('empty', 'twig_test_empty'),
|
||||
new Twig_SimpleTest('iterable', 'twig_test_iterable'),
|
||||
new Twig_SimpleTest('block name', null, array('node_class' => 'Twig_Node_Expression_Test_BlockName')),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,24 +26,32 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression
|
||||
$tag = func_num_args() > 3 ? func_get_arg(3) : null;
|
||||
}
|
||||
|
||||
parent::__construct(array('name' => $name), array('output' => false), $lineno, $tag);
|
||||
parent::__construct(array('name' => $name), array('is_defined_test' => false, 'output' => false), $lineno, $tag);
|
||||
}
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
if ($this->getAttribute('output')) {
|
||||
if ($this->getAttribute('is_defined_test')) {
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write('$this->displayBlock(')
|
||||
->subcompile($this->getNode('name'))
|
||||
->raw(", \$context, \$blocks);\n")
|
||||
;
|
||||
} else {
|
||||
$compiler
|
||||
->raw('$this->renderBlock(')
|
||||
->raw('$this->blockExists(')
|
||||
->subcompile($this->getNode('name'))
|
||||
->raw(', $context, $blocks)')
|
||||
;
|
||||
} else {
|
||||
if ($this->getAttribute('output')) {
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write('$this->displayBlock(')
|
||||
->subcompile($this->getNode('name'))
|
||||
->raw(", \$context, \$blocks);\n")
|
||||
;
|
||||
} else {
|
||||
$compiler
|
||||
->raw('$this->renderBlock(')
|
||||
->subcompile($this->getNode('name'))
|
||||
->raw(', $context, $blocks)')
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks whether a block is defined.
|
||||
*
|
||||
* <pre>
|
||||
* {% if 'title' is block name %}
|
||||
* <title>{{ block('title') }}</title>
|
||||
* {% endif %}
|
||||
* </pre>
|
||||
*
|
||||
* @author Martin Hasoň <martin.hason@gmail.com>
|
||||
*/
|
||||
class Twig_Node_Expression_Test_BlockName extends Twig_Node_Expression_Test
|
||||
{
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
$compiler
|
||||
->raw('$this->blockExists(')
|
||||
->subcompile($this->getNode('node'))
|
||||
->raw(', $context, $blocks)')
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,9 @@ class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
|
||||
$node->setAttribute('is_defined_test', true);
|
||||
} elseif ($node instanceof Twig_Node_Expression_GetAttr) {
|
||||
$node->setAttribute('is_defined_test', true);
|
||||
|
||||
$this->changeIgnoreStrictCheck($node);
|
||||
} elseif ($node instanceof Twig_Node_Expression_BlockReference) {
|
||||
$node->setAttribute('is_defined_test', true);
|
||||
} elseif ($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array) {
|
||||
$node = new Twig_Node_Expression_Constant(true, $node->getTemplateLine());
|
||||
} else {
|
||||
|
||||
+9
-9
@@ -1,14 +1,14 @@
|
||||
--TEST--
|
||||
"block name" test
|
||||
"defined" support for blocks
|
||||
--TEMPLATE--
|
||||
{% extends 'parent' %}
|
||||
{% block icon %}icon{% endblock %}
|
||||
{% block body %}
|
||||
{{ parent() }}
|
||||
{{ 'foo' is block name ? 'ok' : 'ko' }}
|
||||
{{ 'footer' is block name ? 'ok' : 'ko' }}
|
||||
{{ 'icon' is block name ? 'ok' : 'ko' }}
|
||||
{{ 'block1' is block name ? 'ok' : 'ko' }}
|
||||
{{ block('foo') is defined ? 'ok' : 'ko' }}
|
||||
{{ block('footer') is defined ? 'ok' : 'ko' }}
|
||||
{{ block('icon') is defined ? 'ok' : 'ko' }}
|
||||
{{ block('block1') is defined ? 'ok' : 'ko' }}
|
||||
{%- embed 'embed' %}
|
||||
{% block content %}content{% endblock %}
|
||||
{% endembed %}
|
||||
@@ -16,13 +16,13 @@
|
||||
{% use 'blocks' %}
|
||||
--TEMPLATE(parent)--
|
||||
{% block body %}
|
||||
{{ 'icon' is block name ? 'ok' : 'ko' -}}
|
||||
{{ block('icon') is defined ? 'ok' : 'ko' -}}
|
||||
{% endblock %}
|
||||
{% block footer %}{% endblock %}
|
||||
--TEMPLATE(embed)--
|
||||
{{ 'icon' is block name ? 'ok' : 'ko' }}
|
||||
{{ 'content' is block name ? 'ok' : 'ko' }}
|
||||
{{ 'block1' is block name ? 'ok' : 'ko' }}
|
||||
{{ block('icon') is defined ? 'ok' : 'ko' }}
|
||||
{{ block('content') is defined ? 'ok' : 'ko' }}
|
||||
{{ block('block1') is defined ? 'ok' : 'ko' }}
|
||||
--TEMPLATE(blocks)--
|
||||
{% block block1 %}{%endblock %}
|
||||
--DATA--
|
||||
Reference in New Issue
Block a user