Skip to content

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:

crystal
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
end
src/actions/posts/show_post.jbuilder
crystal
json.id post.id
json.title post.title
json.status post.status

The 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:

Actionrenderrender :edit_postrender "posts/partials/post"
Posts::ShowPostposts/show_postposts/edit_postposts/partials/post
Admin::Posts::ShowPostadmin/posts/show_postadmin/posts/edit_postposts/partials/post
Homehomeedit_postposts/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 ​

crystal
render status_code: 201
render :summary, layout: nil
render "dashboard", engine: :water
render "card", dir: "src/components"   # look in another directory
OptionDescription
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:

crystal
Runcobo.views "src/actions", engine: :water   # an HTML site: render uses Water
ArgumentDefaultDescription
rootsrc/actionsWhere templates are looked up
engine::jbuilderThe engine render uses unless told otherwise
layout_dir:<root>/layoutsWhere 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:

crystal
abstract class Blog::BaseAction < ::BaseAction
  layout "api"   # src/actions/layouts/api.jbuilder
end
src/actions/layouts/api.jbuilder
crystal
Jbuilder.new do |json|
  json.data do |json|
    yield_content
  end
end

Rendering json.id 1 through this layout produces {"data":{"id":1}}.

Override or disable the layout per action or per call:

crystal
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
end

By default (on BaseAction) there is no layout.

Jbuilder ​

src/actions/posts/list_posts.jbuilder
crystal
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
json
{"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:

src/actions/posts/partials/summary.jbuilder
crystal
# locals: post
json.id post.id
json.title post.title
src/actions/posts/list_posts.jbuilder
crystal
json.array!("posts", posts) do |json, post|
  json.partial! "posts/partials/summary", post: post
end

Partial 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 ​

src/actions/pages/home.water
crystal
div %|class="hero"| {
  h1 "Welcome, #{user_name}"
  ul {
    items.each do |item|
      li item
    end
  }
}
src/actions/layouts/application.water
crystal
Water.new do
  doctype
  html {
    head { title "My App" }
    body {
      yield_content
    }
  }
end

Text 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:

crystal
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})
end

Outside 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.

Released under the MIT License.