Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Load stuff from map #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/protocols/matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/liquid/template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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