added global variables access in a form theme (closes #3058)

This commit is contained in:
Fabien Potencier 2012-07-10 11:41:19 +02:00
parent 7f0274462f
commit 878e86db8a
8 changed files with 12 additions and 5 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
2.1.0
-----
* added global variables access in a form theme
* added TwigEngine
* added TwigExtractor
* added a csrf_token function

View File

@ -259,9 +259,11 @@ class FormExtension extends \Twig_Extension
if (isset($blocks[$types[$typeIndex]])) {
$this->varStack[$rendering]['typeIndex'] = $typeIndex;
$context = $this->environment->mergeGlobals($this->varStack[$rendering]['variables']);
// we do not call renderBlock here to avoid too many nested level calls (XDebug limits the level to 100 by default)
ob_start();
$this->template->displayBlock($types[$typeIndex], $this->varStack[$rendering]['variables'], $blocks);
$this->template->displayBlock($types[$typeIndex], $context, $blocks);
$html = ob_get_clean();
if ($mainTemplate) {

View File

@ -52,9 +52,10 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
'custom_widgets.html.twig',
));
$environment = new \Twig_Environment($loader);
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension($this->extension);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
$this->extension->initRuntime($environment);
}

View File

@ -52,9 +52,10 @@ class FormExtensionTableLayoutTest extends AbstractTableLayoutTest
'custom_widgets.html.twig',
));
$environment = new \Twig_Environment($loader);
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension($this->extension);
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addGlobal('global', '');
$this->extension->initRuntime($environment);
}

View File

@ -1,3 +1,3 @@
{% block form_label %}
<label>child</label>
<label>{{ global }}child</label>
{% endblock form_label %}

View File

@ -33,6 +33,7 @@ class FormHelperDivLayoutTest extends AbstractDivLayoutTest
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$loader = new FilesystemLoader(array());
$engine = new PhpEngine($templateNameParser, $loader);
$engine->addGlobal('global', '');
$this->helper = new FormHelper($engine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array('FrameworkBundle:Form'));

View File

@ -33,6 +33,7 @@ class FormHelperTableLayoutTest extends AbstractTableLayoutTest
$templateNameParser = new StubTemplateNameParser($root, $rootTheme);
$loader = new FilesystemLoader(array());
$engine = new PhpEngine($templateNameParser, $loader);
$engine->addGlobal('global', '');
$this->helper = new FormHelper($engine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
'FrameworkBundle:Form',

View File

@ -1 +1 @@
<label>child</label>
<label><?php echo $global ?>child</label>