Skip to content

Commit 19568df

Browse files
aswamyclaude
andcommitted
Add for_loop? to Include tag for AST-based keyword detection
Store @is_for_loop during parsing so consumers can determine the with/for keyword from the AST instead of re-parsing raw markup. Matches the existing pattern in the Render tag. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 532b439 commit 19568df

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/liquid/tags/include.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ module Liquid
2020
class Include < Tag
2121
prepend Tag::Disableable
2222

23-
SYNTAX = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
23+
SYNTAX = /(#{QuotedFragment}+)(\s+(with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
2424
Syntax = SYNTAX
25+
FOR = 'for'
2526

2627
attr_reader :template_name_expr, :variable_name_expr, :attributes
2728

@@ -84,12 +85,18 @@ def render_to_output_buffer(context, output)
8485
alias_method :parse_context, :options
8586
private :parse_context
8687

88+
def for_loop?
89+
@is_for_loop
90+
end
91+
8792
def strict2_parse(markup)
8893
p = @parse_context.new_parser(markup)
8994

9095
@template_name_expr = safe_parse_expression(p)
91-
@variable_name_expr = safe_parse_expression(p) if p.id?("for") || p.id?("with")
96+
with_or_for = p.id?("for") || p.id?("with")
97+
@variable_name_expr = safe_parse_expression(p) if with_or_for
9298
@alias_name = p.consume(:id) if p.id?("as")
99+
@is_for_loop = (with_or_for == FOR)
93100

94101
p.consume?(:comma)
95102

@@ -111,11 +118,13 @@ def strict_parse(markup)
111118
def lax_parse(markup)
112119
if markup =~ SYNTAX
113120
template_name = Regexp.last_match(1)
114-
variable_name = Regexp.last_match(3)
121+
with_or_for = Regexp.last_match(3)
122+
variable_name = Regexp.last_match(4)
115123

116-
@alias_name = Regexp.last_match(5)
124+
@alias_name = Regexp.last_match(6)
117125
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
118126
@template_name_expr = parse_expression(template_name)
127+
@is_for_loop = (with_or_for == FOR)
119128
@attributes = {}
120129

121130
markup.scan(TagAttributes) do |key, value|

0 commit comments

Comments
 (0)