Views and Components
Markup goes in a views zone. Each view is a header and a block of markup, and it compiles to a function that returns Html.
Views
The module has to use Ticket.Html. The header is the
function's signature, and the markup below it is its body.
module Articles
uses
Std.List
Models.Article
Paths
Ticket.Html
views
index(articles: List<Article>)
<section>
<h1>Articles</h1>
<ul>{List.map(articles, item)}</ul>
<a href={Paths.new_article_path()}>New article</a>
</section>
item(article: Article)
<li><a href={Paths.article_path(article.id)}>{article.title}</a></li>
exports
index
Holes
{expr} renders any value with a
Render impl, and escapes text. Strings, numbers,
Bool, Float, Html and lists
of them all work.
Attributes
A literal attribute like class="post" is written as is.
An attribute with a hole, name={expr}, goes through the
Attr trait:
| Value | Renders |
|---|---|
String, Int, Id
|
name="escaped value" |
true |
name on its own |
false |
nothing |
None |
nothing |
Some(x) |
whatever x renders |
views
toggle(on: Bool)
<input type="checkbox" checked={on} />
Children and local components
Inside one module, <Card post={p} /> calls the
view card, passing arguments by name. A parameter
called children: Html receives the nested markup.
views
card(post: Post, children: Html)
<article class="post" data-id={post.id}>
<h2>{post.title}</h2>
{children}
</article>
Components across modules
Components shared between modules go in a
view_component zone. A PascalCase header
Button(name: String) becomes
button(props: { name: String }), which you export as
button.
module UI
uses
Ticket.Html
view_component
Button(name: String)
<button>{name}</button>
Card(title: String, children: Html)
<section class="card">
<h2>{title}</h2>
{children}
</section>
exports
button
card
Use them as <UI.Button name="x" /> from any
module that uses UI, or under an alias:
uses
Ticket.Html
UI as Kit
views
wrapped()
<Kit.Card title="T">
<p>body</p>
</Kit.Card>
Layouts
A layout is a plain view that takes the page's Html.
The controller wraps the page itself:
Response.render(Layout.app("Title", Pages.home())).
module Layout
uses
Paths
Ticket.Html
views
app(title: String, body: Html)
<html>
<head>
<title>{title}</title>
</head>
<body>
<nav>
<a href={Paths.root_path()}>Home</a>
<a href={Paths.articles_path()}>Articles</a>
</nav>
<main>{body}</main>
</body>
</html>
exports
app