|
1 | 1 | defmodule PhoenixSlime do
|
| 2 | + @doc """ |
| 3 | + Provides the `~l` sigil with HTML safe Slime syntax inside source files. |
| 4 | +
|
| 5 | + Raises on attempts to use `\#{}`. Use `~L` to allow templating with `\#{}`. |
| 6 | +
|
| 7 | + iex> import PhoenixSlime |
| 8 | + iex> assigns = %{w: "world"} |
| 9 | + iex> ~l"\"" |
| 10 | + ...> p = "hello " <> @w |
| 11 | + ...> "\"" |
| 12 | + {:safe, [[["" | "<p>"] | "hello world"] | "</p>"]} |
| 13 | + """ |
| 14 | + defmacro sigil_l(expr, opts) do |
| 15 | + handle_sigil(expr, opts, __CALLER__.line) |
| 16 | + end |
| 17 | + |
| 18 | + @doc """ |
| 19 | + Provides the `~L` sigil with HTML safe Slime syntax inside source files. |
| 20 | +
|
| 21 | + iex> import PhoenixSlime |
| 22 | + iex> ~L"\"" |
| 23 | + ...> p hello \#{"world"} |
| 24 | + ...> "\"" |
| 25 | + {:safe, [[["" | "<p>hello "] | "world" ] | "</p>"]} |
| 26 | + """ |
| 27 | + defmacro sigil_L(expr, opts) do |
| 28 | + handle_sigil(expr, opts, __CALLER__.line) |
| 29 | + end |
| 30 | + |
| 31 | + defp handle_sigil({:<<>>, _, [expr]}, [], line) do |
| 32 | + expr |
| 33 | + |> Slime.Renderer.precompile() |
| 34 | + |> EEx.compile_string(engine: Phoenix.HTML.Engine, line: line + 1) |
| 35 | + end |
| 36 | + |
| 37 | + defp handle_sigil(_, _, _) do |
| 38 | + raise ArgumentError, ~S(Templating is not allowed with #{} in ~l sigil.) <> |
| 39 | + ~S( Remove the #{}, use = to insert values, or ) <> |
| 40 | + ~S(use ~L to template with #{}.) |
| 41 | + end |
2 | 42 | end
|
0 commit comments