Tutorial — Part 3: Views, Templates & Components¶
In Part 3, you will learn how Orbita's paired views (.tyl + .html + optional .css) work, how layouts wrap pages with <slot />, how reusable components are composed, and how {{ asset(...) }} handles HTTP caching automatically.
1. How Paired Views Work¶
Every route or layout can pair up to three files with the same base name:
index.tyl— server logic returning{ load = fn(ctx), action = fn(ctx), components = { ... } }.index.html— clean HTML template with Angular/Vue-style syntax (@if,@for ... @empty,{{ }},:attr).index.css(optional) — page-specific CSS automatically injected alongside the rendered HTML.
When a GET request arrives, Orbita calls load(ctx), passes the returned table as template variables to index.html (which is compiled once and cached in RAM), and wraps the result in _layout.html.
2. Create the Shared Layout (_layout.html)¶
Edit src/apps/posts/routes/_layout.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Mini Blog — Orbita</title>
<link rel="stylesheet" href="{{ asset('styles.css') }}" />
</head>
<body>
<header class="topbar">
<a href="/" class="brand">Mini Blog</a>
<nav>
@if (ctx.user) {
<a href="/posts/new">New Post</a>
<form method="POST" action="/auth/logout" style="display:inline">
<input type="hidden" name="_csrf" :value="csrf_token" />
<button type="submit">Logout ({{ ctx.user.name }})</button>
</form>
} @else {
<a href="/auth/login">Sign In</a>
}
</nav>
</header>
<main class="container">
@if (flash and flash.success) {
<div class="alert success">{{ flash.success }}</div>
}
@if (flash and flash.error) {
<div class="alert error">{{ flash.error }}</div>
}
<slot />
</main>
<script src="{{ asset('app.js') }}"></script>
</body>
</html>
Notice two built-in helpers:
{{ asset('styles.css') }}: hashespublic/styles.cssand outputs/public/styles.css?v=9f3a1c2d. In production, Orbita serves versioned assets withETag,304 Not Modified, andCache-Control: public, max-age=31536000, immutable.<slot />: inserts the rendered child route HTML here.
3. Build a Reusable Component (post_card)¶
Generate a component with ob g component:
src/apps/posts/components/post_card/post_card.html
<article class="post-card">
<h2><a href="/posts/{{ post.id }}">{{ post.title }}</a></h2>
<p class="meta">By {{ post.author_name }} • {{ post.created_at }}</p>
<p>{{ post.excerpt }}</p>
</article>
src/apps/posts/components/post_card/post_card.tyl
local html = require("ob:html")
return function(props)
return html.template("apps/posts/components/post_card/post_card.html", props)
end
4. Home Page (index.tyl + index.html)¶
Register PostCard in components and query published posts with a JOIN:
src/apps/posts/routes/index.tyl
local Posts = require("../model")
local PostCard = require("../components/post_card/post_card")
return {
components = { PostCard = PostCard },
load = function(ctx)
local rows = Posts
:select("posts.id", "posts.title", "posts.body", "posts.created_at", "users.name AS author_name")
:join("users", "users.id = posts.author_id")
:where("posts.published = ?", 1)
:order_by("posts.id DESC")
:all()
for _, p in ipairs(rows) do
p.excerpt = string.sub(p.body, 1, 140)
end
return { posts = rows }
end,
}
src/apps/posts/routes/index.html
<h1>Latest Posts</h1>
@for (post in posts) {
<PostCard :post="post" />
} @empty {
<p>No posts yet. <a href="/posts/new">Write the first one!</a></p>
}
Template Syntax Cheat Sheet¶
| Syntax | Behavior |
|---|---|
{{ expr }} |
Escaped HTML output (safe against XSS) |
{{{ expr }}} |
Raw unescaped HTML |
@if (cond) { ... } @else if (...) { ... } @else { ... } |
Conditional rendering |
@for (item in list) { ... } @empty { ... } |
Loop with automatic empty fallback |
:attr="expr" |
Dynamic attribute (false/nil omits attribute; true renders boolean attribute) |
<MyComp :prop="val" /> |
Calls a registered component function |
Previous: ← Part 2: Database, Migrations & Models · Next: Part 4: Forms, Auth & Caching →