Skip to content

Commit

Permalink
Support deeply nested paths in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
space-pope committed Oct 5, 2017
1 parent cd774cc commit a51050f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/mustache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ defmodule Mustache do

defp scan_for_dot(template, data) do
regex = regex("{{", "}}", "\\w+(\\.\\w+)+")
scans = Regex.scan(regex, template) |> List.flatten
case scans do
[] -> template
matches = Regex.run(regex, template)
case matches do
nil -> template
_ ->
path = List.first(scans) |> clean(["{{", "}}"])
path = List.first(matches) |> clean(["{{", "}}"])
scan_for_dot(interpolate(template, data, path), data)
end
end
Expand Down Expand Up @@ -110,7 +110,7 @@ defmodule Mustache do
defp strategies do
[{ fn(template) -> Regex.match?(triple_regex(), template) end,
fn(template, data) -> triple_mustaches(template, data) end},
{ fn(template) -> Regex.match?(regex("{{", "}}", "\\w+\\.\\w+"), template) end,
{ fn(template) -> Regex.match?(regex("{{", "}}", "\\w+(\\.\\w+)+"), template) end,
fn(template, data) -> scan_for_dot(template, data) end },
{ fn(template) -> Regex.match?(double_regex(), template) end,
fn(template, data) -> double_mustaches(template, data) end}]
Expand Down
4 changes: 3 additions & 1 deletion test/mustache_feature_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ defmodule MustacheFeatureTest do

test "Dotted Names" do
assert Mustache.render("\"{{person.name}}\" == \"Joe\"",
%{person: %{name: "Joe"}}) == "\"Joe\" == \"Joe\""
%{person: %{name: "Joe"}}) == "\"Joe\" == \"Joe\""
assert Mustache.render("\"{{person.name.first}}\" == \"Joe\"",
%{person: %{name: %{first: "Joe"}}}) == "\"Joe\" == \"Joe\""
end

@tag :pending
Expand Down

0 comments on commit a51050f

Please sign in to comment.