diff --git a/lib/protocols/matcher.ex b/lib/protocols/matcher.ex index c33ec0a3..dbacdf5a 100644 --- a/lib/protocols/matcher.ex +++ b/lib/protocols/matcher.ex @@ -41,7 +41,16 @@ defimpl Liquid.Matcher, for: Map do current |> Liquid.Matcher.match(name, full_context) |> Liquid.Matcher.match(parts, full_context) end - def match(current, key, _full_context) when is_binary(key), do: current[key] + def match(current, key, full_context) when is_binary(key) do + item = + key + |> Liquid.Variable.create() + |> Liquid.Variable.lookup(full_context, []) + |> elem(0) + |> Kernel.||(key) + + current[item] + end end defimpl Liquid.Matcher, for: List do diff --git a/test/liquid/template_test.exs b/test/liquid/template_test.exs index 6e3273b4..0830f2ef 100644 --- a/test/liquid/template_test.exs +++ b/test/liquid/template_test.exs @@ -94,4 +94,28 @@ defmodule Liquid.TemplateTest do assert "from assigns" == rendered assert %{test: "hallo"} == context.registers end + + test "accessing map from liquid" do + t = Liquid.parse_template(:liquid, "{{ foo[bar] }}") + + {:ok, rendered, _} = Liquid.render_template(:liquid, t, %{"foo" => %{ "baz" => "qux" }, "bar" => "baz"}) + + assert "qux" == rendered + end + + test "accessing map by raw key from liquid" do + t = Liquid.parse_template(:liquid, "{{ foo[bar] }}") + + {:ok, rendered, _} = Liquid.render_template(:liquid, t, %{"foo" => %{ "bar" => "qux" }}) + + assert "qux" == rendered + end + + test "when accessing map, looked up key must have priority over raw one" do + t = Liquid.parse_template(:liquid, "{{ foo[bar] }}") + + {:ok, rendered, _} = Liquid.render_template(:liquid, t, %{"foo" => %{ "bar" => "qux", "baz" => "correct" }, "bar" => "baz" }) + + assert "correct" == rendered + end end