Skip to content

Commit

Permalink
recursively parse brackets on variable lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Jan 31, 2023
1 parent daf93a8 commit 1aaf6ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Liquid
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
VariableParser = /\[(?:[^\[\]]+|\g<0>)*\]|#{VariableSegment}+\??/o

RAISE_EXCEPTION_LAMBDA = ->(_e) { raise }

Expand Down
22 changes: 22 additions & 0 deletions test/integration/variable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,26 @@ def test_dynamic_find_var
def test_raw_value_variable
assert_template_result('bar', '{{ [key] }}', { 'key' => 'foo', 'foo' => 'bar' })
end

def test_dynamic_find_var_with_drop
assert_template_result(
'bar',
'{{ [list[settings.zero]] }}',
{
'list' => ['foo'],
'settings' => SettingsDrop.new("zero" => 0),
'foo' => 'bar',
}
)

assert_template_result(
'foo',
'{{ [list[settings.zero]["foo"]] }}',
{
'list' => [{ 'foo' => 'bar' }],
'settings' => SettingsDrop.new("zero" => 0),
'bar' => 'foo',
}
)
end
end
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ def to_liquid
end
end

class SettingsDrop < Liquid::Drop
def initialize(settings)
super()
@settings = settings
end

def liquid_method_missing(key)
@settings[key]
end
end

class IntegerDrop < Liquid::Drop
def initialize(value)
super()
Expand Down

0 comments on commit 1aaf6ed

Please sign in to comment.