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:
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.
Aliases:
nil
blank
numeric
Returns true if the value is a number, i.e. integer or float. Return false if not.
integer
Returns true if the value is an integer, false otherwise.
float
Returns true if the value is an integer, false otherwise.
default
Checks that a variable is defined and returns it. If the variable is not defined, returns the provided default value instead.
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:
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.
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: