Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tags and output

Writing tags

A tag starts at <% and ends at the first %> that follows. Spaces inside the tag are optional, and a tag can span several lines and contain comments:

<%
# Prices in euros
price = 12.5,
quantity = 4
%>Total: <%price * quantity%> euros
Total: 50 euros

A tag contains a single Swan expression. To do several things in one tag, separate the steps with commas (see Sequencing).

From values to text

Each tag is replaced by its value, converted to text:

ValueOutput
a textthe text itself, without quotes
a numberthe number: 42, 3.5, -0.25
a booleanTRUE or FALSE
Nothingnothing at all
a tupleits items, separated by ,
a list[[List of N]]
a namespace[[Namespace of N]]
a function[[Func]]
an undefined value[[Undefined]]
a promiseits value, once it resolves
<% "text" %> | <% 7 / 2 %> | <% 3 > 2 %> | [<% () %>] | <% 1, 2, 3 %>
<% [1, 2, 3] %> | <% {a: 1} %> | <% x -> x %> | <% "a" - 1 %>
text | 3.5 | TRUE | [] | 1, 2, 3
[[List of 3]] | [[Namespace of 1]] | [[Func]] | [[Undefined]]

The bracketed forms tell you that a value has no text form. To show the items of a list, turn it into a tuple with list.enum, or join them with text.join (see Conditions and repetition).

Numbers

Numbers are written with as many digits as needed to represent them exactly, which sometimes shows the limits of floating-point arithmetic:

<% 0.1 + 0.2 %>
<% math.round ((0.1 + 0.2) * 100) / 100 %>
0.30000000000000004
0.3

Round values that come from calculations before showing them.

Tags that write nothing

A tag whose value is Nothing disappears. Assignments return Nothing, so a tag that only binds names writes nothing:

<% greeting = "Hello" %><% greeting %>, world!
Hello, world!

Use : instead of = to bind a name and write its value:

The answer is <% answer : 6 * 7 %>. Again: <% answer %>.
The answer is 42. Again: 42.

Line breaks around tags

The text around a tag is kept exactly, including line breaks. A tag alone on a line leaves an empty line behind:

<% title = "Report" %>
# <% title %>

# Report

To avoid it, put the closing %> right before the next text, as the examples in this manual do:

<% title = "Report" %># <% title %>
# Report

or start the next line inside the tag:

<% title = "Report"
%># <% title %>
# Report

Writing the delimiters as text

There is no escape for <% in the text of a template: it always starts a tag. To write it, produce it from an expression:

Tags look like <% "<" + "%" %> ... <% "%" + ">" %>.
Tags look like <% ... %>.

Inside a tag, %> always ends the tag, even inside a text literal. Split it the same way: "%" + ">".

Swan text literals cannot contain a double quote. Write quotes in the text around the tags instead:

<% name = "Ada" %>She said "<% name %>".
She said "Ada".

Unfinished tags

If a <% has no matching %>, the rest of the template, from the <% on, is copied to the output as it is.