mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-18 13:26:37 +00:00
Deprecate usage of undefined blocks
This commit is contained in:
@@ -197,12 +197,16 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
|||||||
$block = null;
|
$block = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null !== $template) {
|
// avoid RCEs when sandbox is enabled
|
||||||
// avoid RCEs when sandbox is enabled
|
if (null !== $template && !$template instanceof self) {
|
||||||
if (!$template instanceof self) {
|
throw new LogicException('A block must be a method on a Twig_Template instance.');
|
||||||
throw new LogicException('A block must be a method on a Twig_Template instance.');
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!$this->hasBlock($name, $context, $blocks)) {
|
||||||
|
@trigger_error(sprintf('Displaying undefined block "%s" in template "%s" is deprecated since version 1.29 and will throw an exception in 2.0.', $name, $this->getTemplateName()), E_USER_DEPRECATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null !== $template) {
|
||||||
try {
|
try {
|
||||||
$template->$block($context, $blocks);
|
$template->$block($context, $blocks);
|
||||||
} catch (Twig_Error $e) {
|
} catch (Twig_Error $e) {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
{% block list %}<ul>{{ block('children') }}</ul>{% endblock %}
|
{% block list %}<ul>{{ block('children') }}</ul>{% endblock %}
|
||||||
{% block children %}{% set currentItem = item %}{% for item in currentItem %}{{ block('item') }}{% endfor %}{% set item = currentItem %}{% endblock %}
|
{% block children %}{% set currentItem = item %}{% for item in currentItem %}{{ block('item') }}{% endfor %}{% set item = currentItem %}{% endblock %}
|
||||||
{% block item %}<li>{% if item is not iterable %}{{ block('label') }}{% else %}{{ block('list') }}{% endif %}</li>{% endblock %}
|
{% block item %}<li>{% if item is not iterable %}{{ block('label') }}{% else %}{{ block('list') }}{% endif %}</li>{% endblock %}
|
||||||
{% block label %}{{ item }}{{ block('unknown') }}{% endblock %}
|
{% block label %}{{ item }}{% endblock %}
|
||||||
--TEMPLATE(base.twig)--
|
--TEMPLATE(base.twig)--
|
||||||
{{ block('list') }}
|
{{ block('list') }}
|
||||||
--DATA--
|
--DATA--
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
--TEST--
|
||||||
|
"block" function with undefined block
|
||||||
|
--TEMPLATE--
|
||||||
|
{% extends "base.twig" %}
|
||||||
|
{% block foo %}{{ parent() }}{{ block('unknown') }}{{ block('bar') }}{% endblock %}
|
||||||
|
--TEMPLATE(base.twig)--
|
||||||
|
{% block foo %}Foo{% endblock %}
|
||||||
|
{% block bar %}Bar{% endblock %}
|
||||||
|
--DATA--
|
||||||
|
return array()
|
||||||
|
--EXPECT--
|
||||||
|
FooBarBar
|
||||||
@@ -214,15 +214,17 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertNotInstanceof('Twig_Markup', $template->getAttribute($template1, 'empty'));
|
$this->assertNotInstanceof('Twig_Markup', $template->getAttribute($template1, 'empty'));
|
||||||
$this->assertSame('', $template->getAttribute($template1, 'empty'));
|
$this->assertSame('', $template->getAttribute($template1, 'empty'));
|
||||||
|
|
||||||
|
$blocks = array('name' => array($template1, 'block_name'));
|
||||||
|
|
||||||
// trigger some deprecation notice messages to check them with @expectedDeprecation
|
// trigger some deprecation notice messages to check them with @expectedDeprecation
|
||||||
$template->getAttribute($template, 'renderBlock', array('name', array()));
|
$template->getAttribute($template, 'renderBlock', array('name', array(), $blocks));
|
||||||
$template->getAttribute($template, 'displayBlock', array('name', array()));
|
$template->getAttribute($template, 'displayBlock', array('name', array(), $blocks));
|
||||||
$template->getAttribute($template, 'hasBlock', array('name', array()));
|
$template->getAttribute($template, 'hasBlock', array('name', array()));
|
||||||
$template->getAttribute($template, 'render', array(array()));
|
$template->getAttribute($template, 'render', array(array()));
|
||||||
$template->getAttribute($template, 'display', array(array()));
|
$template->getAttribute($template, 'display', array(array()));
|
||||||
|
|
||||||
$template->getAttribute($template1, 'renderBlock', array('name', array()));
|
$template->getAttribute($template1, 'renderBlock', array('name', array(), $blocks));
|
||||||
$template->getAttribute($template1, 'displayBlock', array('name', array()));
|
$template->getAttribute($template1, 'displayBlock', array('name', array(), $blocks));
|
||||||
$template->getAttribute($template1, 'hasBlock', array('name', array()));
|
$template->getAttribute($template1, 'hasBlock', array('name', array()));
|
||||||
$template->getAttribute($template1, 'render', array(array()));
|
$template->getAttribute($template1, 'render', array(array()));
|
||||||
$template->getAttribute($template1, 'display', array(array()));
|
$template->getAttribute($template1, 'display', array(array()));
|
||||||
@@ -233,6 +235,20 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($template->getAttribute($template1, 'displayWithErrorHandling', array(), Twig_Template::METHOD_CALL, true));
|
$this->assertFalse($template->getAttribute($template1, 'displayWithErrorHandling', array(), Twig_Template::METHOD_CALL, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group legacy
|
||||||
|
* @expectedDeprecation Displaying undefined block "unknown" in template "index.twig" is deprecated since version 1.29 and will throw an exception in 2.0.
|
||||||
|
* @expectedDeprecation Displaying undefined block "unknown" in template "index.twig" is deprecated since version 1.29 and will throw an exception in 2.0.
|
||||||
|
*/
|
||||||
|
public function testRenderBlockWithUndefinedBlock()
|
||||||
|
{
|
||||||
|
$twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock());
|
||||||
|
|
||||||
|
$template = new Twig_TemplateTest($twig, false, 'index.twig');
|
||||||
|
$template->renderBlock('unknown', array());
|
||||||
|
$template->displayBlock('unknown', array());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getTestsDependingOnExtensionAvailability
|
* @dataProvider getTestsDependingOnExtensionAvailability
|
||||||
*/
|
*/
|
||||||
@@ -534,6 +550,10 @@ class Twig_TemplateTest extends Twig_Template
|
|||||||
return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
|
return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function block_name($context, array $blocks = array())
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Twig_TemplateArrayAccessObject implements ArrayAccess
|
class Twig_TemplateArrayAccessObject implements ArrayAccess
|
||||||
|
|||||||
Reference in New Issue
Block a user