2023-01-21 12:08:22 +00:00
![Temple ](temple-github-image.png )
2019-04-15 01:44:39 +00:00
2019-11-23 03:51:38 +00:00
[![Actions Status ](https://github.com/mhanberg/temple/workflows/CI/badge.svg )](https://github.com/mhanberg/temple/actions)
2019-07-04 02:05:50 +00:00
[![Hex.pm ](https://img.shields.io/hexpm/v/temple.svg )](https://hex.pm/packages/temple)
2019-06-30 18:39:05 +00:00
2023-01-21 12:08:22 +00:00
> You are looking at the README for the main branch. The README for the latest stable release is located [here](https://github.com/mhanberg/temple/tree/v0.11.0).
# Temple
2020-06-16 19:50:18 +00:00
2022-09-20 00:35:45 +00:00
Temple is an Elixir DSL for writing HTML and SVG.
2019-04-15 01:44:39 +00:00
## Installation
2019-07-04 02:05:50 +00:00
Add `temple` to your list of dependencies in `mix.exs` :
2019-04-15 01:44:39 +00:00
2023-06-14 02:23:26 +00:00
<!-- x - release - please - start - version -->
2019-04-15 01:44:39 +00:00
```elixir
def deps do
2021-01-02 18:21:48 +00:00
[
2023-06-14 02:23:26 +00:00
{:temple, "~> 0.12"}
2021-01-02 18:21:48 +00:00
]
2020-11-05 00:58:24 +00:00
end
```
2023-06-14 02:24:24 +00:00
<!-- x - release - please - end -->
2023-06-14 02:23:26 +00:00
2021-04-11 21:27:02 +00:00
## Goals
2021-04-12 00:19:34 +00:00
Currently Temple has the following things on which it won't compromise.
2021-04-11 21:27:02 +00:00
2021-04-12 00:19:34 +00:00
- Will only work with valid Elixir syntax.
2022-04-20 03:56:46 +00:00
- Should work in all web environments such as Plug, Aino, Phoenix, and Phoenix LiveView.
2021-04-11 21:27:02 +00:00
2019-06-30 18:39:05 +00:00
## Usage
2022-04-20 03:56:46 +00:00
Using Temple is as simple as using the DSL inside of an `temple/1` block. The runtime result of the macro is your HTML.
2019-07-04 02:05:50 +00:00
2022-04-20 03:56:46 +00:00
See the [guides ](https://hexdocs.pm/temple/your-first-template.html ) for more details.
2019-07-04 02:05:50 +00:00
2019-06-30 18:39:05 +00:00
```elixir
2022-04-20 03:56:46 +00:00
import Temple
2019-06-30 18:39:05 +00:00
2019-07-09 02:29:41 +00:00
temple do
2020-06-16 19:28:21 +00:00
h2 do: "todos"
2019-06-30 18:39:05 +00:00
ul class: "list" do
for item < - @items do
li class: "item" do
div class: "checkbox" do
div class: "bullet hidden"
end
2020-06-16 19:28:21 +00:00
div do: item
2019-06-30 18:39:05 +00:00
end
end
end
2020-06-16 19:28:21 +00:00
script do: """
2019-06-30 18:39:05 +00:00
function toggleCheck({currentTarget}) {
currentTarget.children[0].children[0].classList.toggle("hidden");
}
let items = document.querySelectorAll("li");
Array.from(items).forEach(checkbox => checkbox.addEventListener("click", toggleCheck));
"""
end
```
2020-07-16 02:31:22 +00:00
### Components
2022-04-20 03:56:46 +00:00
Temple components are simple to write and easy to use.
2020-07-25 16:07:00 +00:00
2022-04-20 03:56:46 +00:00
Unlike normal partials, Temple components have the concept of "slots", which are similar [Vue ](https://v3.vuejs.org/guide/component-slots.html#named-slots ). You can also refer to HEEx and Surface for examples of templates that have the "slot" concept.
2020-07-16 02:31:22 +00:00
2022-10-12 13:17:23 +00:00
Temple components are compatible with HEEx and Surface components and can be shared.
2022-04-20 03:56:46 +00:00
Please see the [guides ](https://hexdocs.pm/temple/components.html ) for more details.
2020-07-24 19:54:38 +00:00
2022-04-20 03:56:46 +00:00
```elixir
defmodule MyAppWeb.Component do
import Temple
def card(assigns) do
temple do
section do
div do
2022-10-12 13:17:23 +00:00
slot @header
2022-04-20 03:56:46 +00:00
end
2021-05-13 01:40:12 +00:00
2022-04-20 03:56:46 +00:00
div do
2022-10-12 13:17:23 +00:00
slot @inner_block
2022-04-20 03:56:46 +00:00
end
2021-05-13 01:40:12 +00:00
2022-04-20 03:56:46 +00:00
div do
2022-10-12 13:17:23 +00:00
slot @footer
2022-04-20 03:56:46 +00:00
end
2021-05-13 01:40:12 +00:00
end
2020-07-24 19:54:38 +00:00
end
end
2020-07-16 02:31:22 +00:00
end
```
2022-04-20 03:56:46 +00:00
Using components is as simple as passing a reference to your component function to the `c` keyword.
2020-07-16 02:31:22 +00:00
```elixir
2022-04-20 03:56:46 +00:00
import MyAppWeb.Component
2021-01-02 18:21:48 +00:00
2022-04-20 03:56:46 +00:00
c & card/1 do
2021-05-13 01:40:12 +00:00
slot :header do
@user .full_name
end
@user .bio
slot :footer do
a href: "https://twitter.com/#{@user.twitter}" do
"@#{@user.twitter}"
end
a href: "https://github.com/#{@user.github}" do
"@#{@user.github}"
end
end
2020-07-16 02:31:22 +00:00
end
```
2022-04-20 03:56:46 +00:00
### Engine
2019-06-30 18:39:05 +00:00
2022-04-20 03:56:46 +00:00
By default, Temple will use the `EEx.SmartEngine` that is built into the Elixir standard library. If you are a web framework that uses it's own template engine (such as [Aino ](https://github.com/oestrich/aino ) and Phoenix/LiveView, you can configure Temple to it!
2019-06-30 18:39:05 +00:00
```elixir
2022-04-20 03:56:46 +00:00
# config/config.exs
2019-06-30 18:39:05 +00:00
2022-04-20 03:56:46 +00:00
config :temple,
engine: Aino.View.Engine # or Phoenix.HTML.Engine or Phoenix.LiveView.Engine
2019-06-30 18:39:05 +00:00
```
2019-07-09 00:06:01 +00:00
### Formatter
To include Temple's formatter configuration, add `:temple` to your `.formatter.exs` .
```elixir
[
import_deps: [:temple],
2020-07-14 14:03:42 +00:00
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs,lexs}"],
2019-07-09 00:06:01 +00:00
]
```
2021-04-16 04:16:00 +00:00
2022-04-20 03:56:46 +00:00
## Phoenix
2023-05-26 14:45:09 +00:00
When using Phoenix ~> 1.7, all you need to do is include `:temple` in your mix.exs.
If you plan on using the template structure that < 1.6 Phoenix applications use , you can use `:temple_phoenix` as described below .
2022-04-20 03:56:46 +00:00
To use with [Phoenix ](https://github.com/phoenixframework/phoenix ), please use the [temple_phoenix ](https://github.com/mhanberg/temple_phoenix ) package! This bundles up some useful helpers as well as the Phoenix Template engine.
2021-04-16 04:16:00 +00:00
## Related
- [Introducing Temple: An elegant HTML library for Elixir and Phoenix ](https://www.mitchellhanberg.com/introducing-temple-an-elegant-html-library-for-elixir-and-phoenix/ )
- [Temple, AST, and Protocols ](https://www.mitchellhanberg.com/temple-ast-and-protocols/ )
2022-04-20 03:56:46 +00:00
- [Thinking Elixir Episode 92: Temple with Mitchell Hanberg ](https://podcast.thinkingelixir.com/92 )
- [How EEx Turns Your Template Into HTML ](https://www.mitchellhanberg.com/how-eex-turns-your-template-into-html/ )