Skip to content
David Ciulla edited this page Nov 7, 2026 · 1 revision

Views

Laravex renders views with a Blade-compatible template engine. View files live in resources/views/ and are located by name using dot notation. The .blade.php extension is preferred; .html and .eex are also supported as fallbacks.

Creating Views

Place template files in resources/views/:

resources/views/
  welcome.blade.php
  posts/
    index.blade.php
    show.blade.php
  layouts/
    app.blade.php
  components/
    alert.blade.php

Rendering Views

Call Laravex.View.make/2 from a route handler or controller. It returns a Laravex.Http.Response with Content-Type: text/html:

alias Laravex.View

def index(_request) do
  posts = App.Models.Post.all()
  View.make("posts.index", %{posts: posts})
end

def show(request) do
  post = request.params["post"]
  View.make("posts.show", %{post: post, title: post["title"]})
end

The view/2 helper (available via use App.Http.Controllers.Controller) is an alias for View.make/2:

view("posts.index", %{posts: posts})

Passing Data

Pass any Elixir map as the second argument. Keys are stringified, so both atom and string keys work in templates:

View.make("dashboard", %{
  user:   current_user,
  stats:  %{posts: 12, comments: 44},
  recent: recent_posts
})

Access data in templates with {{ $key }} or {{ key }} (the $ prefix is optional).

Shared Data

Errors, old input, flash messages, and the CSRF token are merged into every view automatically by the ShareErrors and ShareFlash middleware and View.merge_shared/1:

Key Description
errors Validation error map from the previous request
old Input values from the previous request
flash Flash messages aged for this request
_token / csrf_token CSRF token from the session

You do not need to pass these manually.

View Location

View.make/2 searches for the view file in this order:

  1. resources/views/{name}.blade.php
  2. resources/views/components/{name}.blade.php
  3. resources/views/{name}.html
  4. resources/views/{name}.eex

Dot notation maps to directory separators: "posts.show" → resources/views/posts/show.blade.php.

View Caching

Rendered content is cached at the file level by Laravex.View.Cache. The cache is automatically invalidated when the source file changes.

Rendering Strings

For programmatic rendering without a file, use View.render_string/2:

html = Laravex.View.render_string("<h1>{{ title }}</h1>", %{"title" => "Hello"})

This passes through the full Blade compiler pipeline. See Blade-Templates for the complete directive reference.

Clone this wiki locally