A view engine for rendering EEx templates. Complete demo application is in "demo" subfolder
def deps do
[
{:eex_render, "~> 0.1"},
]
enddefmodule Demo do
use Plug.Router
use EExRender,
templates: ["lib/template/"],
template_ext: ".html.eex", # if not set default is ".html.eex"
layout: "layout", # lib/template/layout.html.eex
helpers: [Helpers] # default is []
get "/" do
conn
|> assign(:key1, "val1") # accessible in template as `@key1` or `@assigns[:key]`
|> render("home") # lib/template/home.html.eex
end
end- All
**/*.html.eexfiles inlib/templatewill be precompiled to EEx template functions. - Subfolders stay in template name, e.g. "partial/menu"
layout.html.eexis set as default layout, therefore it must exists and contain<%= @main_content %>- All functions in
Helpersmodule will be available in templates - Custom template extension can be used, e.g.
template_ext: ".eex"
This way render(conn, "home") will render home.html.eex inside layout.html.eex and send as HTML response
render(conn, html: "<b>html</b>")send HTML fragment (without layout)render(conn, json: %{a: 1, b: "json"})send JSONrender(conn, text: "OK")send text/plainrender(conn, text: "Not Found", status: 404, content_type: "vnd/error")custom status/content_typerender(conn, "full_error_page", layout: false, status: 404)renderfull_error_page.html.eexwithout layoutrender(conn, "error_message", layout: "error_page", status: 404)rendererror_message.html.eexwith custom layouterror_page.html.eex
render("partial/menu", key1: 11, key2: 22)renderpartial/menu.html.eexinside current template- given locals available as
@key1and@key2 - all locals are in
@assigns, presence can be checked with@assigns[:key1] - all given helper modules are imported - their functions can be called directly