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]], or its own text
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).

Namespaces with their own text

A namespace can say how it is written: give it a member __text__, a function that receives the namespace and returns a text.

<% force = {value: 120, unit: "kN", __text__: f -> text.from f.value + " " + f.unit} %>
Horizontal load: <% Fx: force %>
Factored: <% 1.35 * Fx.value %> kN

Horizontal load: 120 kN
Factored: 162 kN

The tag <% Fx: force %> shows the force as text and also names it, so the next line can still compute with Fx.value. This is how a data table in Stilo shows as a table and still gives its columns to other lines.

__text__ must return a text. If it returns anything else, such as a number, the tag shows [[Undefined]].

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.