From a51050ffebe1a5c3208cf6a557e499599894a3c0 Mon Sep 17 00:00:00 2001 From: Josh Ziegler Date: Wed, 4 Oct 2017 20:57:05 -0400 Subject: [PATCH] Support deeply nested paths in templates --- lib/mustache.ex | 10 +++++----- test/mustache_feature_test.exs | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/mustache.ex b/lib/mustache.ex index 88d4672..925d2b2 100644 --- a/lib/mustache.ex +++ b/lib/mustache.ex @@ -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 @@ -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}] diff --git a/test/mustache_feature_test.exs b/test/mustache_feature_test.exs index 55265e2..e800fc2 100644 --- a/test/mustache_feature_test.exs +++ b/test/mustache_feature_test.exs @@ -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