---
url: /runcobo/guide/views.md
description: >-
  Render JSON with Jbuilder and HTML with Water templates, using layouts and
  partials compiled into your app.
---

# Views

Runcobo renders templates with two engines:

* **[Jbuilder](https://github.com/shootingfly/jbuilder)** (`.jbuilder`) — builds JSON with a Ruby-like DSL.
* **[Water](https://github.com/shootingfly/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](/guide/project-structure#one-endpoint-three-files) — `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](#configuration) 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
```

```crystal [src/actions/posts/show_post.jbuilder]
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:

| 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

```crystal
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:

```crystal
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:

```crystal
abstract class Blog::BaseAction < ::BaseAction
  layout "api"   # src/actions/layouts/api.jbuilder
end
```

```crystal [src/actions/layouts/api.jbuilder]
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

```crystal [src/actions/posts/list_posts.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
```

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

```crystal [src/actions/posts/partials/summary.jbuilder]
# locals: post
json.id post.id
json.title post.title
```

```crystal [src/actions/posts/list_posts.jbuilder]
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](https://github.com/shootingfly/jbuilder) for the full DSL.

## Water

```crystal [src/actions/pages/home.water]
div %|class="hero"| {
  h1 "Welcome, #{user_name}"
  ul {
    items.each do |item|
      li item
    end
  }
}
```

```crystal [src/actions/layouts/application.water]
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](https://github.com/shootingfly/water) 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.
