No description
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.
Find a file
dependabot[bot] cfb2a325b9
chore: Bump ex_doc from 0.29.1 to 0.30.0 (#206)
Bumps [ex_doc](https://github.com/elixir-lang/ex_doc) from 0.29.1 to 0.30.0.
- [Changelog](https://github.com/elixir-lang/ex_doc/blob/main/CHANGELOG.md)
- [Commits](https://github.com/elixir-lang/ex_doc/compare/v0.29.1...v0.30.0)

---
updated-dependencies:
- dependency-name: ex_doc
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-07-07 08:48:37 -04:00
.github ci: release-please readme 2023-06-13 22:23:26 -04:00
bin Compile to EEx (#80) 2020-06-16 15:28:21 -04:00
config Utilize the EEx Engine instead of creating an EEx string (#177) 2022-04-19 23:56:46 -04:00
guides feat!: configure runtime attributes function (#202) 2023-06-12 23:38:16 -04:00
integration_test/temple_plug_demo Utilize the EEx Engine instead of creating an EEx string (#177) 2022-04-19 23:56:46 -04:00
lib feat!: configure runtime attributes function (#202) 2023-06-12 23:38:16 -04:00
test feat!: configure runtime attributes function (#202) 2023-06-12 23:38:16 -04:00
.formatter.exs Dynamic Attributes (#190) 2023-01-21 06:44:29 -05:00
.gitignore Compile to EEx (#80) 2020-06-16 15:28:21 -04:00
.tool-versions feat: Mix task to convert HTML into Temple (#180) 2022-09-11 22:39:31 -04:00
CHANGELOG.md chore(main): release 0.12.0 (#204) 2023-06-12 23:49:12 -04:00
LICENSE feat: New Component API 2021-01-02 13:22:03 -05:00
mix.exs chore: Bump ex_doc from 0.29.1 to 0.30.0 (#206) 2023-07-07 08:48:37 -04:00
mix.lock chore: Bump ex_doc from 0.29.1 to 0.30.0 (#206) 2023-07-07 08:48:37 -04:00
README.md ci: fix release-please readme 2023-06-13 22:24:24 -04:00
temple-github-image.png Utilize the EEx Engine instead of creating an EEx string (#177) 2022-04-19 23:56:46 -04:00
temple.png Logo 2019-07-04 00:16:37 -04:00

Temple

Actions Status Hex.pm

You are looking at the README for the main branch. The README for the latest stable release is located here.

Temple

Temple is an Elixir DSL for writing HTML and SVG.

Installation

Add temple to your list of dependencies in mix.exs:

def deps do
  [
    {:temple, "~> 0.12"}
  ]
end

Goals

Currently Temple has the following things on which it won't compromise.

  • Will only work with valid Elixir syntax.
  • Should work in all web environments such as Plug, Aino, Phoenix, and Phoenix LiveView.

Usage

Using Temple is as simple as using the DSL inside of an temple/1 block. The runtime result of the macro is your HTML.

See the guides for more details.

import Temple

temple do
  h2 do: "todos"

  ul class: "list" do
    for item <- @items do
      li class: "item" do
        div class: "checkbox" do
          div class: "bullet hidden"
        end

        div do: item
      end
    end
  end

  script do: """
  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

Components

Temple components are simple to write and easy to use.

Unlike normal partials, Temple components have the concept of "slots", which are similar Vue. You can also refer to HEEx and Surface for examples of templates that have the "slot" concept.

Temple components are compatible with HEEx and Surface components and can be shared.

Please see the guides for more details.

defmodule MyAppWeb.Component do
  import Temple

  def card(assigns) do
    temple do
      section do
        div do
          slot @header
        end

        div do
          slot @inner_block
        end

        div do
          slot @footer
        end
      end
    end
  end
end

Using components is as simple as passing a reference to your component function to the c keyword.

import MyAppWeb.Component

c &card/1 do
  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
end

Engine

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 and Phoenix/LiveView, you can configure Temple to it!

# config/config.exs

config :temple,
  engine: Aino.View.Engine # or Phoenix.HTML.Engine or Phoenix.LiveView.Engine

Formatter

To include Temple's formatter configuration, add :temple to your .formatter.exs.

[
  import_deps: [:temple],
  inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs,lexs}"],
]

Phoenix

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.

To use with Phoenix, please use the temple_phoenix package! This bundles up some useful helpers as well as the Phoenix Template engine.