Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Antlers: stops double-initial execution of tags within conditions #9504

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading