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

Conversation

JohnathonKoster
Copy link
Contributor

@JohnathonKoster JohnathonKoster commented Feb 10, 2024

This PR resolves a bug when using tags inside conditions. Currently, it will evaluate a tag twice on initial usage. This existing bug can be observed by using a template similar to the following:

{{ if {switch between='yes|no'} == 'yes' }}yes{{ else }}no{{ /if }}
{{ if {switch between='yes|no'} == 'yes' }}yes{{ else }}no{{ /if }}

Currently, all evaluations would print no since the tag with execute once when building up the context before evaluating the condition, and would execute again when evaluating the condition itself. This PR improves this internal behavior to prevent double execution for the initial tag execution.

Existing (valid) behaviors have not been changed, and additional test coverage added to ensure they are working correctly.

Interpolations are still processed when evaluating conditions:

{{ if 'some_value' == 'some_{variable_name}' }}yes{{ else }}no{{ /if }}

If tags are used multiple times in the condition, they are still executed each time they are used. Test coverage added to ensure we are not using the cached value of the initial evaluation:

{{ if 'one_{switch between='yes|no'}' == 'one_yes' && 'two_{switch between='yes|no'}' == 'two_no' }}yes{{ else }}no{{ /if }}

* Only prevent the *initial* second evaluation of the tag;
* Explicitly calling the tag multiple times should evaluate them that many times;
* Add test coverage for resolving tags when adjusting values;
* Add test coverage for resolving tags when adjusting interpolated results multiple times in a single condition
@jasonvarga jasonvarga merged commit 76bd972 into statamic:4.x Feb 16, 2024
22 checks passed
@jasonvarga jasonvarga deleted the antlers-prevent-executing-tags-twice-in-conditions branch February 16, 2024 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants