-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
1 parent
99c7884
commit a92b033
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
defmodule PhoenixSlime do | ||
@doc """ | ||
Provides the `~l` sigil with HTML safe Slime syntax inside source files. | ||
Raises on attempts to use `\#{}`. Use `~L` to allow templating with `\#{}`. | ||
iex> import PhoenixSlime | ||
iex> assigns = %{w: "world"} | ||
iex> ~l"\"" | ||
...> p = "hello " <> @w | ||
...> "\"" | ||
{:safe, [[["" | "<p>"] | "hello world"] | "</p>"]} | ||
""" | ||
defmacro sigil_l(expr, opts) do | ||
handle_sigil(expr, opts, __CALLER__.line) | ||
end | ||
|
||
@doc """ | ||
Provides the `~L` sigil with HTML safe Slime syntax inside source files. | ||
iex> import PhoenixSlime | ||
iex> ~L"\"" | ||
...> p hello \#{"world"} | ||
...> "\"" | ||
{:safe, [[["" | "<p>hello "] | "world" ] | "</p>"]} | ||
""" | ||
defmacro sigil_L(expr, opts) do | ||
handle_sigil(expr, opts, __CALLER__.line) | ||
end | ||
|
||
defp handle_sigil({:<<>>, _, [expr]}, [], line) do | ||
expr | ||
|> Slime.Renderer.precompile() | ||
|> EEx.compile_string(engine: Phoenix.HTML.Engine, line: line + 1) | ||
end | ||
|
||
defp handle_sigil(_, _, _) do | ||
raise ArgumentError, ~S(Templating is not allowed with #{} in ~l sigil.) <> | ||
~S( Remove the #{}, use = to insert values, or ) <> | ||
~S(use ~L to template with #{}.) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters