Skip to content

Functions overview

Templates provide a number of functions that manipulate constants and variables. Each data type has its own set of functions, which you can call using the dot (.) notation, for example:

<%= "lowercase".upper %>
LOWERCASE

Functions

General helpers

These functions can be called on any value, irrespective of data type.

null

Returns true if the value is null, false if not.

<h1>
  <% if title.null %>
    Unnamed
  <% else %>
    <%= title %>
  <% end %>
</h1>

Aliases:

  • nil
  • blank

numeric

Returns true if the value is a number, i.e. integer or float. Return false if not.

<% if value.numeric %>
  <input type="number">
<% else %>
  <input type="text">
<% end %>

integer

Returns true if the value is an integer, false otherwise.

<% 5.integer == true %>

float

Returns true if the value is an integer, false otherwise.

<% 5.float == false %>

default

Checks that a variable is defined and returns it. If the variable is not defined, returns the provided default value instead.

<%= default(some_var, "default_value") %>

If some_var variable is not defined:

default_value
If some_var is set to "value":
value

Global helpers

Global functions are standalone and are not called on a value. They are used to generate some useful code in the template.

rwf_head

Inserts JavaScript into template that makes Rwf work smoothly. Currently this function downloads and initializes Hotwired Turbo and Stimulus libraries. As the name of the function suggests, it's best used inside the <head> element, for example:

<!doctype html>
<html>
  <head>
    <%- rwf_head() %>
  </head>
  <body>
    <!-- ... -->

rwf_turbo_stream

Inserts JavaScript code which will create and initialize a Turbo Stream WebSocket connection. Use this function inside the <body> element1:

<!doctype html>
<html>
  <head>
    <%- rwf_head() %>
  </head>
  <body>
    <%- rwf_turbo_stream("/turbo-stream") %>
    <!-- ... -->

render

Renders a template directly inside the current template. Can be used for rendering partials. <%% is a special template code tag which is an alias for render.

<div>
  <%- render("templates/profile.html") %>
</div>

<!-- The same as: -->

<div>
  <%% "templates/profile.html" %>
</div>

csrf_token

Renders an input field with a valid CSRF token.

<form action="/login" method="post">
    <%= csrf_token() %>
</form>

csrf_token_raw

Renders a valid CSRF token as a raw HTML string. It can then be passed to JavaScript via a data- attribute or a global variable:

<div data-csrf-token="<%= csrf_token_raw() %>"
</div>