Skip to content

Commit

Permalink
[4.x] Antlers: stops double-initial execution of tags within conditio…
Browse files Browse the repository at this point in the history
…ns (#9504)
  • Loading branch information
JohnathonKoster authored Feb 16, 2024
1 parent c16b576 commit 76bd972
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/View/Antlers/Language/Runtime/ConditionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public function process(ConditionNode $node, $data)
}

if ($result == true) {

$this->processor->setIsConditionProcessor($condValueToRestore);

return $branch;
Expand Down
6 changes: 5 additions & 1 deletion src/View/Antlers/Language/Runtime/Sandbox/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,11 @@ public function getValue($val)
$varName = $this->nameOf($val);

if ($val->isInterpolationReference) {
$interpolationValue = $this->adjustValue($this->nodeProcessor->reduceInterpolatedVariable($val), $val);
if (array_key_exists($varName->normalizedReference, $this->data)) {
$interpolationValue = $this->adjustValue($this->data[$varName->normalizedReference], $val);
} else {
$interpolationValue = $this->adjustValue($this->nodeProcessor->reduceInterpolatedVariable($val), $val);
}

// If the currently active node is an instance of ArithmeticNodeContract,
// we will ask the runtime type coercion to convert whatever value
Expand Down
54 changes: 54 additions & 0 deletions tests/Antlers/Sandbox/ConditionalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Antlers\Sandbox;

use Statamic\Tags\Tags;
use Tests\Antlers\ParserTestCase;

class ConditionalsTest extends ParserTestCase
Expand Down Expand Up @@ -47,6 +48,59 @@ public function test_sandbox_will_defer_collapsing_arrays()
$this->assertSame('no', $this->renderString($template, $data));
}

public function test_tags_are_not_initially_evaluated_twice()
{
$template = <<<'EOT'
{{ if {switch between='yes|no'} == 'yes' }}yes{{ else }}no{{ /if }}{{ if {switch between='yes|no'} == 'yes' }}yes{{ else }}no{{ /if }}
EOT;

$this->assertSame('yesno', $this->renderString($template, [], true));
}

public function test_tags_are_evaluated_twice_if_called_twice()
{
$template = <<<'EOT'
{{ if {switch between='yes|no'} == 'yes' && {switch between='yes|no'} == 'no' }}yes{{ else }}no{{ /if }}{{ if {switch between='yes|no'} == 'yes' && {switch between='yes|no'} == 'no' }}yes{{ else }}no{{ /if }}{{ if {switch between='yes|no'} == 'no' && {switch between='yes|no'} == 'yes' }}yes{{ else }}no{{ /if }}
EOT;

$this->assertSame('yesyesno', $this->renderString($template, [], true));
}

public function test_value_adjustments_inside_of_conditionals()
{
$template = <<<'EOT'
{{ if 'bob' == 'bo{c}' }}yes{{ else }}no{{ /if }}
EOT;
$this->assertSame('yes', $this->renderString($template, ['c' => 'b'], false));
}

public function test_many_tag_value_adjustments_inside_of_conditionals()
{
$template = <<<'EOT'
{{ if 'one_{switch between='yes|no'}' == 'one_yes' && 'two_{switch between='yes|no'}' == 'two_no' }}yes{{ else }}no{{ /if }}
EOT;

$this->assertSame('yes', $this->renderString($template, [], true));
}

public function test_value_adjustments_resolved_tag_values()
{
(new class extends Tags
{
protected static $handle = 'the_tag';

public function index()
{
return 'bob';
}
})::register();

$template = <<<'EOT'
{{ if 'bob' == '{the_tag}' }}yes{{ else }}no{{ /if }}
EOT;
$this->assertSame('yes', $this->renderString($template, [], true));
}

public function test_sandbox_evaluates_simple_boolean_expressions()
{
$result = $this->getBoolResult('true == false', []);
Expand Down

0 comments on commit 76bd972

Please sign in to comment.