From 7763223bde128f5a05be7ffdb5f936174d0b945a Mon Sep 17 00:00:00 2001 From: Drew Knab Date: Thu, 15 Jan 2026 18:50:07 -0500 Subject: [PATCH] support flat data --- lib/mustache.ex | 11 ++++++----- test/mustache_feature_test.exs | 7 +++++++ test/mustache_integration_test.exs | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/mustache.ex b/lib/mustache.ex index e5cfa14..cf93c1c 100644 --- a/lib/mustache.ex +++ b/lib/mustache.ex @@ -41,14 +41,14 @@ defmodule Mustache do [full, predicate, var, body] -> val = indifferent_access(data, var) section_val = case predicate do - "#" -> process_if(body, val) - "^" -> process_unless(body, val) + "#" -> process_if(body, val, data) + "^" -> process_unless(body, val, data) end process_section(String.replace(template, full, section_val), data) end end - defp process_if(template, val) do + defp process_if(template, val, data) do case val do nil -> "" false -> "" @@ -56,14 +56,15 @@ defmodule Mustache do [_ | _] -> val |> Stream.map(&(render(template, &1))) |> Enum.join() + true -> render(template, data) val -> render(template, val) end end - defp process_unless(template, val) do + defp process_unless(template, val, data) do case val do nil -> render(template, val) - false -> render(template, val) + false -> render(template, data) [] -> render(template, val) _val -> "" end diff --git a/test/mustache_feature_test.exs b/test/mustache_feature_test.exs index 322fe96..9e59f1c 100644 --- a/test/mustache_feature_test.exs +++ b/test/mustache_feature_test.exs @@ -98,6 +98,13 @@ defmodule MustacheFeatureTest do %{people: [%{name: "Joe"}, %{name: "Jill"}]}) == "\"Joe Jill \"" end + test "Section Interpolation - Flat Object" do + template = "\"{{#mph}}{{speed}} mph{{/mph}}{{^mph}}{{speed}} kph{{/mph}}\"" + + assert Mustache.render(template, %{mph: true, speed: 24}) == "\"24 mph\"" + assert Mustache.render(template, %{mph: false, speed: 24}) == "\"24 kph\"" + end + #Whitespace sensitivity test "Interpolation - Surrounding Whitespace" do diff --git a/test/mustache_integration_test.exs b/test/mustache_integration_test.exs index ce3f1d2..c205c15 100644 --- a/test/mustache_integration_test.exs +++ b/test/mustache_integration_test.exs @@ -42,6 +42,6 @@ defmodule MustacheTest do """ actual = Mustache.render(template, data) - assert String.strip(actual) == String.strip(expected) + assert String.trim(actual) == String.trim(expected) end end