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/README.md

147 lines
3.6 KiB
Markdown
Raw Normal View History

2019-07-04 04:16:37 +00:00
# ![](temple.png)
2019-04-15 01:44:39 +00:00
2020-06-16 19:28:21 +00:00
> You are looking at the README for the master branch. The README for the latest stable release is located [here](https://github.com/mhanberg/temple/tree/v0.5.0).
2020-04-21 15:24: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-08-26 19:31:48 +00:00
[![Slack](https://img.shields.io/badge/chat-Slack-blue)](https://elixir-lang.slack.com/messages/CMH6MA4UD)
2019-06-30 18:39:05 +00:00
2019-07-02 02:48:51 +00:00
Temple is a DSL for writing HTML using Elixir.
2019-06-30 18:39:05 +00:00
2020-06-16 19:28:21 +00:00
You're probably here because you want to use Temple to write Phoenix templates, which is why Temple includes a [Phoenix template engine](#phoenix-templates).
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
```elixir
def deps do
2020-06-16 19:28:21 +00:00
[{:temple, "~> 0.6.0-alpha.0"}]
2019-04-15 01:44:39 +00:00
end
```
2019-06-30 18:39:05 +00:00
## Usage
2020-06-16 19:28:21 +00:00
Using Temple is a as simple as using the DSL inside of an `temple/1` block. This returns an EEx string at compile time.
2019-07-04 02:05:50 +00:00
See the [documentation](https://hexdocs.pm/temple/Temple.Html.html) for more details.
2019-07-04 02:05:50 +00:00
2019-06-30 18:39:05 +00:00
```elixir
2019-07-02 02:48:51 +00:00
use 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
```
### Phoenix templates
Add the templating engine to your Phoenix configuration.
2020-06-16 19:28:21 +00:00
See the [Temple.Engine](https://hexdocs.pm/temple/Temple.Engine.html#content) and [Temple.LiveEngine](https://hexdocs.pm/temple/Temple.LiveEngine.html#content) for more details.
2019-07-04 02:05:50 +00:00
2019-06-30 18:39:05 +00:00
```elixir
# config.exs
2020-06-16 19:28:21 +00:00
config :phoenix, :template_engines,
exs: Temple.Engine
exs: Temple.LiveEngine
2019-06-30 18:39:05 +00:00
# config/dev.exs
config :your_app, YourAppWeb.Endpoint,
live_reload: [
patterns: [
2020-06-16 19:28:21 +00:00
~r"lib/myapp_web/(live|views)/.*(ex|exs)$",
~r"lib/myapp_web/templates/.*(eex|exs)$"
]
]
2019-06-30 18:39:05 +00:00
```
```elixir
# app.html.exs
2020-06-16 19:28:21 +00:00
2019-06-30 18:39:05 +00:00
html lang: "en" do
head do
meta charset: "utf-8"
meta http_equiv: "X-UA-Compatible", content: "IE=edge"
meta name: "viewport", content: "width=device-width, initial-scale=1.0"
2020-06-16 19:28:21 +00:00
title do: "YourApp · Phoenix Framework"
2019-06-30 18:39:05 +00:00
link rel: "stylesheet", href: Routes.static_path(@conn, "/css/app.css")
end
body do
header do
section class: "container" do
nav role: "navigation" do
ul do
li do: a("Get Started", href: "https://hexdocs.pm/phoenix/overview.html")
end
end
a href: "http://phoenixframework.org/", class: "phx-logo" do
img src: Routes.static_path(@conn, "/images/phoenix.png"),
alt: "Phoenix Framework Logo"
end
end
end
main role: "main", class: "container" do
2020-06-16 19:28:21 +00:00
p class: "alert alert-info", role: "alert" do
get_flash(@conn, :info)
end
p class: "alert alert-danger", role: "alert" do
get_flash(@conn, :error)
end
2019-06-30 18:39:05 +00:00
2020-06-16 19:28:21 +00:00
render @view_module, @view_template, assigns
2019-06-30 18:39:05 +00:00
end
script type: "text/javascript", src: Routes.static_path(@conn, "/js/app.js")
end
end
```
### Tasks
2020-06-16 19:28:21 +00:00
#### temple.gen.layout
Generates the app layout.
2020-06-16 19:28:21 +00:00
#### temple.gen.html
2020-06-16 19:28:21 +00:00
Generates the templates for a resource.
### Formatter
To include Temple's formatter configuration, add `:temple` to your `.formatter.exs`.
```elixir
[
import_deps: [:temple],
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
]
```