Skip to content

Commit e429c94

Browse files
committed
Use VariableLookup instead of dot notation
1 parent 6372289 commit e429c94

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/liquid/standardfilters.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,10 +1007,10 @@ def fetch_property(drop, property_or_keys)
10071007

10081008
return value if !value.nil? || !property_or_keys.is_a?(String)
10091009

1010-
keys = property_or_keys.split('.')
1011-
keys.reduce(drop) do |drop, key|
1012-
drop.respond_to?(:[]) ? drop[key] : drop
1013-
end
1010+
variable_lookup = Liquid::VariableLookup.parse("drop.#{property_or_keys}")
1011+
variable_lookup.evaluate(
1012+
Liquid::Context.new("drop" => drop),
1013+
)
10141014
end
10151015

10161016
def raise_property_error(property)

test/integration/standard_filter_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,30 @@ def test_sum_with_deep_enumerables
13371337
assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
13381338
end
13391339

1340+
def test_access_arrays
1341+
template = <<~LIQUID
1342+
{{- products | map: 'colors[0]["name"]' | join: ", " -}}
1343+
LIQUID
1344+
expected_output = "red, red, blue"
1345+
1346+
assert_template_result(expected_output, template, {
1347+
"products" => [
1348+
{ "title" => { "content" => "Pro goggles" }, "colors" => [{ "name" => "red", "hex" => "#ff0000" }] },
1349+
{ "title" => { "content" => "Thermal gloves" }, "colors" => [{ "name" => "red", "hex" => "#ff0000" }] },
1350+
{ "title" => { "content" => "Alpine jacket" }, "colors" => [{ "name" => "blue", "hex" => "#0000ff" }] },
1351+
],
1352+
})
1353+
end
1354+
1355+
def test_access_lookup_with_brackets
1356+
template = <<~LIQUID
1357+
{{- products | map: 'title["content"]' | join: ', ' -}}
1358+
LIQUID
1359+
expected_output = "Pro goggles, Thermal gloves, Alpine jacket, Mountain boots, Safety helmet"
1360+
1361+
assert_template_result(expected_output, template, { "products" => TestDeepEnumerable.new })
1362+
end
1363+
13401364
private
13411365

13421366
def with_timezone(tz)

0 commit comments

Comments
 (0)