feat: add FOREACH_LOCALISE option to scope loop variables#419
Open
toddr-bot wants to merge 1 commit into
Open
Conversation
Add a FOREACH_LOCALISE configuration option that causes FOREACH loops with named target variables to save and restore the target variable's value after the loop exits, matching Perl's foreach behavior. Without this option (the default), the loop variable retains its last iteration value after the loop — the historical TT2 behavior. With FOREACH_LOCALISE => 1: [% x = 'original' %] [% FOREACH x = [1, 2, 3] %]...[% END %] [% x %] => 'original' (not '3') The option can be set per-template via Template->new() or globally via $Template::Directive::FOREACH_LOCALISE. Fixes GH #317. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
Author
|
Recreated from #346 (auto-closed when the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add a
FOREACH_LOCALISEconfiguration option that restores the loop iterator variable to its pre-loop value after FOREACH exits.Why
Fixes GH #317. Currently
FOREACH x = listleavesxset to the last iteration value after the loop — inconsistent with Perl'sforeachwhich localizes the iterator. This surprises users coming from Perl.Making this unconditional would break backward compatibility (some code relies on reading the variable after the loop, e.g. with
LAST), so this is opt-in via a config option.How
loopsave/restore inDirective::foreach()Template->new()→Parser→Directivefactory$Template::Directive::FOREACH_LOCALISETesting
t/foreach-localise.tcovering: basic restore, undef initial value, nested loops, hash variables, postfix form🤖 Generated with Claude Code