Skip to content

Tags

Code block

You can write Rust statement inside <% %> tag.

<%
let mut total = 0;
for i in 1.. {
    total += i;
    if i > 100 {
        break;
    }
}
%>
<div>total = <%= total %></div>
<div>total = 105</div>

Note

Make sure that you cannot omit braces, parenthesis, and semicolons.

Sailfish is smart enough to figure out where the code block ends, so you can even include %> inside Rust comments or string literals.

<% /* Tag does not ends at %>! */ %>

If you need to simply render <% character, you can escape it, or use evaluation block (described below).

<%% is converted into <%- "<%" %> character.
<% is converted into <% character

Although almost all Rust statement is supported, the following statements inside templates may cause a strange compilation error.

  • Function/Macro definition that render some contents
  • impl item
  • Macro call which defines some local variable.
  • Macro call which behaviour depends on the path to source file
  • Generator expression (yield)

Evaluation block

Rust expression inside <%= %> tag is evaluated and the result will be rendered.

<% let a = 1; %><%= a + 2 %>
3

If the result contains &"'<> characters, sailfish replaces these characters with the equivalent html.

If you want to render the results without escaping, you can use <%- %> tag or configure sailfish to not escape by default.

<div>
  <%- "<h1>Hello, World!</h1>" %>
</div>
<div>
  <h1>Hello, World!</h1>
</div>

Note

Evaluation block does not return any value, so you cannot use the block to pass the render result to another code block. The following code is invalid.

<% let result = %><%= 1 %><% ; %>

Component block

Rust expression inside <%+ %> tag is evaluated and then rendered by calling its render_once() method. If the value does not have an appropriate method, a compile-time error will be reported.

This makes it easy to use types which are TemplateOnce as components which can be embedded into other templates.

<strong>A <%= val %></strong>
B <%+ A { val: "example" } %>
B <strong>A example</strong>