This repository has been archived on 2023-08-07. You can view files and clone it, but cannot push or open issues or pull requests.
temple/lib/temple/engine.ex

45 lines
1.1 KiB
Elixir
Raw Normal View History

2019-07-02 02:48:51 +00:00
defmodule Temple.Engine do
2019-04-29 02:25:57 +00:00
@behaviour Phoenix.Template.Engine
2021-01-02 18:21:48 +00:00
@moduledoc """
The Temple HTML engine makes it possible to use Temple with Phoenix controllers.
To get started, you will configure Phoenix to use this module for `.exs` files.
```elixir
# config.exs
config :phoenix, :template_engines,
# this will work for files named like `index.html.exs`
exs: Temple.Engine
# config/dev.exs
config :your_app, YourAppWeb.Endpoint,
live_reload: [
patterns: [
~r"lib/myapp_web/(live|views)/.*(ex|exs|lexs)$",
~r"lib/myapp_web/templates/.*(eex|exs|lexs)$"
]
]
# my_app/
# lib/
# my_app/
# my_app_web/
# templates/
# posts/
# show.html.exs
```
Now you can get started by writing `exs` files in the templates directory and they will be compiled as you would expect.
"""
2019-06-29 22:14:22 +00:00
2019-04-29 02:25:57 +00:00
def compile(path, _name) do
2020-06-16 19:28:21 +00:00
require Temple
2019-04-29 02:25:57 +00:00
2020-06-16 19:28:21 +00:00
template = path |> File.read!() |> Code.string_to_quoted!(file: path)
2019-04-29 02:25:57 +00:00
2021-01-02 18:21:48 +00:00
Temple.temple(template)
2020-06-16 19:28:21 +00:00
|> EEx.compile_string(engine: Phoenix.HTML.Engine, file: path, line: 1)
2019-04-29 02:25:57 +00:00
end
end