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

201 lines
5.4 KiB
Markdown
Raw Normal View History

2019-07-04 04:16:37 +00:00
# ![](temple.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-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
2020-06-16 19:50:18 +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).
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-07-16 02:47:23 +00:00
[{:temple, "~> 0.6.0-alpha.2"}]
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
```
2020-07-16 02:31:22 +00:00
### Components
To define a component, you can create a file in your configured temple
components directory, which defaults to `lib/components`. You would
probably want to change that to be `lib/my_app_web/components` if you
are building a phoenix app.
```elixir
# config/config.exs
config :temple, :components_path, "./lib/my_app_web/components"
```
This file should be of the `.exs` extension.
You can then use this component in any other temple template.
For example, if I were to define a `flex` component, I would create a
file called `lib/my_app_web/components/flex.exs`, with the following
contents.
```elixir
div class: "flex #{@temple[:class]}", id: @id do
@children
end
```
And we could use the component like so
```elixir
flex class: "justify-between items-center", id: "arnold" do
div do: "Hi"
div do: "I'm"
div do: "Arnold"
div do: "Schwarzenegger"
end
```
We've demonstrated several features to components in this example.
We can pass assigns to our component, and access them just like we would in a normal phoenix template. If they don't match up with any assigns we passed to our component, they will be rendered as-is, and will become a normal Phoenix assign.
You can also access a special `@temple` assign. This allows you do optionally pass an assign, and not have the `@my_assign` pass through. If you didn't pass it to your component, it will evaluate to nil.
The block passed to your component can be accessed as `@children`. This allows your components to wrap a body of markup from the call site.
In order for components to trigger a recompile when they are changed, you can call `use Temple.Recompiler` in your `lib/my_app_web.ex` file, in the `view`, `live_view`, and `live_component` functions
```elixir
def view do
quote do
# ...
use Temple.Recompiler
# ...
end
end
```
2019-06-30 18:39:05 +00:00
### Phoenix templates
Add the templating engine to your Phoenix configuration.
```elixir
# config.exs
2020-06-16 19:28:21 +00:00
config :phoenix, :template_engines,
exs: Temple.Engine
2020-06-16 21:36:12 +00:00
# or for LiveView support
2020-07-01 00:40:38 +00:00
# this will work for files named like `index.html.lexs`
# you can enable Elixir syntax highlighting in your editor
lexs: Temple.LiveViewEngine
2019-06-30 18:39:05 +00:00
# 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)$"
]
]
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:50:18 +00:00
p class: "alert alert-info", role: "alert", compact: true, do: get_flash(@conn, :info)
p class: "alert alert-danger", role: "alert", compact: true, do: get_flash(@conn, :error)
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,lexs}"],
]
```