Essentials / Helpers

Docs Essentials

Helpers

Turbo Laravel has a set of template functions and Rust helpers that make working with Turbo easier.

DOM IDs

Turbo Streams and Turbo Frames find elements by their DOM ID, so it helps to name the elements that render a model consistently. Every model has a dom_id and a dom_class through the DomId trait:

rust
use turbo::DomId;

comment.dom_id();                // "comment_7"
comment.dom_id_with("edit");     // "edit_comment_7"
Comment::new_dom_id();           // "new_comment"
Comment::dom_class();            // "comment"
Comment::dom_class_with("card"); // "card_comment"

The names come from the model's morph name, which is its struct name in snake_case unless #[model(morph = "…")] names another one.

To use them in templates, write #[turbo::record_identifier] above #[model]:

rust
use laravel::model::model;

#[turbo::record_identifier]
#[model]
pub struct Comment {
    pub id: i64,
    pub post_id: i64,
    pub body: String,
}

Templates get the same functions:

jinja
<div id="{{ dom_id(comment) }}" class="{{ dom_class(comment, 'card') }}">
    {{ comment.body }}
</div>

<turbo-frame id="{{ dom_id(post, 'comments') }}">...</turbo-frame>

The attribute adds #[serde(tag = "$model")] to the model, so the model's serialized form names its type. That's how the template functions know a value is a Comment. The tag also appears in the model's JSON responses.

Models with #[turbo::broadcasts] are already identified, so they don't need the attribute.

For values that aren't marked models, dom_id also takes a name and a value:

jinja
{{ dom_id("post", 42) }} {# post_42 #}

The Frame Macro

Turbo Laravel ships a frame template macro that renders a <turbo-frame> with a model's DOM ID:

jinja
{% from "turbo::components" import frame %}

{% call frame(post, "comments") %}
    <p>Loading...</p>
{% endcall %}

The frame's ID is dom_id(post, "comments"), so comments_post_42 here. You may also pass a string ID, and any other attribute with attrs, such as to make it a lazy-loading frame:

jinja
{% call frame(post, "comments", attrs={"src": route("comments::index", post), "loading": "lazy"}) %}
    <p>Loading...</p>
{% endcall %}

{{ frame("sidebar") }}

Page Configuration

These functions render the meta tags that configure Turbo for a page, usually in the layout's <head>:

Function Renders
turbo_refreshes_with(method="morph", scroll="preserve") How Turbo refreshes the page: replace (the default) or morph, and whether to reset (the default) or preserve the scroll position.
turbo_exempts_page_from_cache() <meta name="turbo-cache-control" content="no-cache">: Turbo doesn't cache the page.
turbo_exempts_page_from_preview() <meta name="turbo-cache-control" content="no-preview">: Turbo doesn't show a cached preview of the page.
turbo_page_requires_reload() <meta name="turbo-visit-control" content="reload">: visiting the page always reloads it.
turbo_page_view_transition() <meta name="view-transition" content="same-origin">: uses the View Transitions API between pages.

For example:

jinja
<head>
    {{ turbo_refreshes_with(method="morph", scroll="preserve") }}
    {% block head %}{% endblock %}
</head>

Subscribing to Broadcasts

turbo_stream_from subscribes the page to a channel's broadcasts:

jinja
{{ turbo_stream_from(post) }}                {# private-posts.42 #}
{{ turbo_stream_from(post, type="public") }} {# posts.42 #}
{{ turbo_stream_from("dashboard") }}         {# private-dashboard #}

Hotwire Native

is_native() tells whether the request comes from a Hotwire Native app, so you may hide parts of the page the app shows natively:

jinja
{% if not is_native() %}
    <nav>...</nav>
{% endif %}

Request Helpers

The RequestExt trait adds Turbo's request headers to laravel's Request:

rust
use laravel::http::Request;
use turbo::RequestExt;

request.wants_turbo_stream();             // `Accept` lists text/vnd.turbo-stream.html
request.turbo_frame();                    // Option<&str>, from `Turbo-Frame`
request.was_from_turbo_frame("comments"); // the request came from this frame
request.turbo_request_id();               // Option<&str>, from `X-Turbo-Request-Id`
request.is_native();                      // a Hotwire Native app sent the request