Skip to content

Commit a92b033

Browse files
the-mikedavisRakoth
authored andcommitted
Sigil L for safe Slime HTML (#63)
* Initial ~l and ~L macros * Document * Test * Escape the octothorpe so it's included in the docs * Fix bad test * Interpolation -> Templating in docs and error msg
1 parent 99c7884 commit a92b033

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/phoenix_slime.ex

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
11
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
242
end

test/phoenix_slime_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule PhoenixSlimeTest do
22
use ExUnit.Case
33
alias Phoenix.View
4+
doctest PhoenixSlime
45

56
defmodule MyApp.PageView do
67
use Phoenix.View, root: "test/fixtures/templates"

0 commit comments

Comments
 (0)