Views
Runcobo renders templates with two engines:
- Jbuilder (
.jbuilder) — builds JSON with a Ruby-like DSL. - Water (
.water) — writes HTML in plain Crystal.
Templates are compiled into your binary. Missing templates and layouts are compile errors, and you don't ship template files to production.
Setup: templates next to actions
Each action's template sits beside the action in its business area folder — posts/show_post.cr renders posts/show_post.jbuilder. Runcobo looks for templates in src/actions and layouts in src/actions/layouts, with Jbuilder as the engine. There's nothing to configure; change the defaults only if you need to.
Rendering
Call render with no arguments to render the template named after the action:
class Posts::ShowPost < Blog::BaseAction
get "/posts/:id"
call do
input = validate(ShowPostContract)
if post = store.get_post?(id: input.id)
render # → src/actions/posts/show_post.jbuilder
else
render_json({error: "post_not_found"}, status_code: 404)
end
end
endjson.id post.id
json.title post.title
json.status post.statusThe template is compiled into the action's method, so it can use the local variables defined in call (post above), instance variables and the action's methods — including store.
How the template is found
The action's class name, converted to snake_case, gives the path under the view root:
| Action | render | render :edit_post | render "posts/partials/post" |
|---|---|---|---|
Posts::ShowPost | posts/show_post | posts/edit_post | posts/partials/post |
Admin::Posts::ShowPost | admin/posts/show_post | admin/posts/edit_post | posts/partials/post |
Home | home | edit_post | posts/partials/post |
A name without / is looked up in the action's own folder; a name with / is relative to the view root. The engine's extension (.jbuilder or .water) is added for you.
Options
render status_code: 201
render :summary, layout: nil
render "dashboard", engine: :water
render "card", dir: "src/components" # look in another directory| Option | Description |
|---|---|
status_code: | Response status (default 200) |
layout: | A layout name, or nil for none (default: inherited, see below) |
engine: | :jbuilder or :water (default: the configured engine) |
dir: | Look up the template in this directory instead |
layout_dir: | Look up the layout in this directory instead |
Jbuilder responses get Content-Type: application/json; Water responses get text/html.
Configuration
The defaults fit a standard project. To change them, call Runcobo.views once, before any action is defined:
Runcobo.views "src/actions", engine: :water # an HTML site: render uses Water| Argument | Default | Description |
|---|---|---|
| root | src/actions | Where templates are looked up |
engine: | :jbuilder | The engine render uses unless told otherwise |
layout_dir: | <root>/layouts | Where layouts are looked up |
Paths are relative to the project root (where you run crystal build). A second call is a compile error.
Layouts
A layout wraps every template rendered with it. Set one with layout on an action — usually on a base class, since layouts are inherited:
abstract class Blog::BaseAction < ::BaseAction
layout "api" # src/actions/layouts/api.jbuilder
endJbuilder.new do |json|
json.data do |json|
yield_content
end
endRendering json.id 1 through this layout produces {"data":{"id":1}}.
Override or disable the layout per action or per call:
class Feeds::Show < Blog::BaseAction
layout nil # no layout for this action and its subclasses
call do
render layout: "export" # or choose one for a single render
end
endBy default (on BaseAction) there is no layout.
Jbuilder
json.count posts.size
json.array!("posts", posts) do |json, post|
json.id post.id
json.title post.title
end
json.meta do |json|
json.generated_at Time.utc.to_s
end{"count":2,"posts":[{"id":1,"title":"A"},{"id":2,"title":"B"}],"meta":{"generated_at":"..."}}Partials
Extract repeated fragments into partials. A partial declares the locals it needs on its first line:
# locals: post
json.id post.id
json.title post.titlejson.array!("posts", posts) do |json, post|
json.partial! "posts/partials/summary", post: post
endPartial paths are relative to the view root. Missing partials and missing or extra locals are compile errors.
Put an area's shared templates in its partials/ folder, as above.
See the Jbuilder README for the full DSL.
Water
div %|class="hero"| {
h1 "Welcome, #{user_name}"
ul {
items.each do |item|
li item
end
}
}Water.new do
doctype
html {
head { title "My App" }
body {
yield_content
}
}
endText passed to tags is HTML-escaped, so h1 user_name is safe with untrusted input. See the Water README for the full DSL.
Rendering to a string
render_to_string takes the same arguments as render but returns the output instead of writing the response — useful for emails, caching or embedding:
call do
html = render_to_string :digest, engine: :water, layout: nil
# hand `html` to your mailer, store it in a cache, ...
render_json({length: html.bytesize})
endOutside an action, use Runcobo.render_to_string "path/name" with a path relative to the view root.
Shortcuts
render_jbuilder "posts/show_post" and render_water "pages/home" pick the engine explicitly and always resolve the name from the view root.