Add test to check if a block exists

This commit is contained in:
Martin Hasoň
2015-09-15 23:45:28 +02:00
committed by Fabien Potencier
parent c577631f39
commit 4f013e09ea
6 changed files with 121 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
``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 %}
+1
View File
@@ -4,6 +4,7 @@ Tests
.. toctree::
:maxdepth: 1
block_name
constant
defined
divisibleby
+1
View File
@@ -222,6 +222,7 @@ 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')),
);
}
@@ -0,0 +1,33 @@
<?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)')
;
}
}
+35
View File
@@ -286,6 +286,8 @@ abstract class Twig_Template implements Twig_TemplateInterface
*
* @return bool true if the block exists, false otherwise
*
* @see blockExists
*
* @internal
*/
public function hasBlock($name)
@@ -653,4 +655,37 @@ abstract class Twig_Template implements Twig_TemplateInterface
return $ret;
}
/**
* Returns whether a block exists or not in the current context of the template.
*
* This method checks blocks defined in the current template
* or defined in "used" traits or defined in parent templates.
*
* @param string $name The block name
* @param array $context The context
* @param array $blocks The current set of blocks
*
* @return bool true if the block exists, false otherwise
*
* @see hasBlock
*
* @internal
*/
protected function blockExists($name, array $context, array $blocks = array())
{
if (isset($blocks[$name])) {
return $blocks[$name][0] instanceof self;
}
if (isset($this->blocks[$name])) {
return true;
}
if (false !== $parent = $this->getParent($context)) {
return $parent->blockExists($name, $context);
}
return false;
}
}
@@ -0,0 +1,38 @@
--TEST--
"block name" test
--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' }}
{%- embed 'embed' %}
{% block content %}content{% endblock %}
{% endembed %}
{% endblock %}
{% use 'blocks' %}
--TEMPLATE(parent)--
{% block body %}
{{ 'icon' is block name ? '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' }}
--TEMPLATE(blocks)--
{% block block1 %}{%endblock %}
--DATA--
return array()
--EXPECT--
ok
ko
ok
ok
ok
ko
ok
ko