Skip to content

Commit

Permalink
[Form] Fixed variable passing from outer to inner blocks of the same …
Browse files Browse the repository at this point in the history
…FormView instance
  • Loading branch information
webmozart committed Jul 25, 2012
1 parent 965fe32 commit fb002d8
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 53 deletions.
44 changes: 33 additions & 11 deletions src/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php
Expand Up @@ -32,22 +32,44 @@ public function compile(\Twig_Compiler $compiler)
$compiler->raw(', \'' . $blockNameSuffix . '\'');

if (isset($arguments[1])) {
$compiler->raw(', ');

// The "label" function allows one extra argument here, the label
if ('label' === $blockNameSuffix) {
if (isset($arguments[2])) {
$compiler->subcompile($arguments[2]);
$compiler->raw(' + ');
// The "label" function expects the label in the second argument.
// The array of variables is given in the third argument
$lineno = $arguments[1]->getLine();
$variables = new \Twig_Node_Expression_Array(array(), $lineno);
$givenVariables = isset($arguments[2]) ? $arguments[2] : $variables;
$labelKey = new \Twig_Node_Expression_Constant('label', $lineno);
$found = false;

// If the label is listed in the variables, the label given
// in the arguments should take precedence in the following form:
// labelInArgs|default(labelInAttr)
foreach ($givenVariables->getKeyValuePairs() as $pair) {
if ((string) $labelKey === (string) $pair['key']) {
$pair['value'] = new \Twig_Node_Expression_Filter_Default(
$arguments[1],
new \Twig_Node_Expression_Constant('default', $lineno),
new \Twig_Node(array($pair['value']), array(), $lineno),
$lineno
);
$found = true;
}

$variables->addElement($pair['value'], $pair['key']);
}

// Add the label to the variable array
$compiler->raw('array(\'label\' => ');
$compiler->subcompile($arguments[1]);
$compiler->raw(')');
// If the label does not exist in the variables, simply add it
if (!$found) {
$variables->addElement($arguments[1], $labelKey);
}
} else {
$compiler->subcompile($arguments[1]);
// All other functions than "label" expect the variables
// in the second argument
$variables = $arguments[1];
}

$compiler->raw(', ');
$compiler->subcompile($variables);
}
}

Expand Down
Expand Up @@ -249,7 +249,7 @@
{% block form_row %}
{% spaceless %}
<div>
{{ form_label(form, label|default(null)) }}
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
Expand Down
Expand Up @@ -4,7 +4,7 @@
{% spaceless %}
<tr>
<td>
{{ form_label(form, label|default(null)) }}
{{ form_label(form) }}
</td>
<td>
{{ form_errors(form) }}
Expand Down
187 changes: 187 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Node/SearchAndRenderBlockNodeTest.php
@@ -0,0 +1,187 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Tests\Node;

use Symfony\Bridge\Twig\Tests\TestCase;
use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode;

class SearchAndRenderBlockNodeTest extends TestCase
{
protected function setUp()
{
parent::setUp();

if (version_compare(\Twig_Environment::VERSION, '1.5.0', '<')) {
$this->markTestSkipped('Requires Twig version to be at least 1.5.0.');
}
}

public function testCompileWidget()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
));

$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

public function testCompileWidgetWithVariables()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
), 0),
));

$node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'widget\', array("foo" => "bar"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

public function testCompileLabelWithLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant('my label', 0),
));

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => "my label"))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

public function testCompileLabelWithNullLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant(null, 0),
));

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("label" => null))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

public function testCompileLabelWithDefaultLabel()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
));

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\')',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

public function testCompileLabelWithAttributes()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant(null, 0),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
), 0),
));

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => null))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

public function testCompileLabelWithLabelAndAttributes()
{
$arguments = new \Twig_Node(array(
new \Twig_Node_Expression_Name('form', 0),
new \Twig_Node_Expression_Constant('value in argument', 0),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 0),
new \Twig_Node_Expression_Constant('bar', 0),
new \Twig_Node_Expression_Constant('label', 0),
new \Twig_Node_Expression_Constant('value in attributes', 0),
), 0),
));

$node = new SearchAndRenderBlockNode('form_label', $arguments, 0);

$compiler = new \Twig_Compiler(new \Twig_Environment());

$this->assertEquals(
sprintf(
'$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(%s, \'label\', array("foo" => "bar", "label" => _twig_default_filter("value in argument", "value in attributes")))',
$this->getVariableGetter('form')
),
trim($compiler->compile($node)->getSource())
);
}

protected function getVariableGetter($name)
{
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
}

return sprintf('$this->getContext($context, "%s")', $name);
}
}
@@ -1,5 +1,5 @@
<div>
<?php echo $view['form']->label($form, isset($label) ? $label : null) ?>
<?php echo $view['form']->label($form) ?>
<?php echo $view['form']->errors($form) ?>
<?php echo $view['form']->widget($form) ?>
</div>
@@ -1,6 +1,6 @@
<tr>
<td>
<?php echo $view['form']->label($form, isset($label) ? $label : null) ?>
<?php echo $view['form']->label($form) ?>
</td>
<td>
<?php echo $view['form']->errors($form) ?>
Expand Down
Expand Up @@ -125,7 +125,7 @@ public function row(FormView $view, array $variables = array())
*/
public function label(FormView $view, $label = null, array $variables = array())
{
if ($label !== null) {
if (null !== $label) {
$variables += array('label' => $label);
}

Expand Down

0 comments on commit fb002d8

Please sign in to comment.