Skip to content

Commit

Permalink
Merge pull request #2 from pylon/deep-nesting
Browse files Browse the repository at this point in the history
Support deeply nested paths in templates
  • Loading branch information
space-pope authored Oct 5, 2017
2 parents fb95395 + a51050f commit b3ab5d4
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 @@ -75,11 +75,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 @@ -151,7 +151,7 @@ defmodule Mustache do
fn(template, data) -> process_section(template, data) end },
{ 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

#Sections
Expand Down

0 comments on commit b3ab5d4

Please sign in to comment.