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

String Template

String templates are strings with dynamic components that can be rendered for each node based on the node attributes.

A simple template can be like below:

Hi, my name is {name}, my address is {address}.

Results (with: name=John; address=123 Road, USA):

Hi, my name is John, my address is 123 Road, USA.

With more complicated templates, we would be able to generate documents with text and images based on the node attributes as well.

For example the following template can be used to generate a table.

| Name             | Index   |
|------------------|---------|
<!-- ---8<--- -->
| {NAME} | {INDEX} |
<!-- ---8<--- -->
network load_file("./data/mississippi.net");
network echo(render_template("./data/example.template"))

Results:

NameIndex
lower-mississippi0
red1
arkansas2
missouri3
upper-mississippi4
ohio5
tenessee6

Of course, there are better ways to generate table than this, but this shows how flexible the template system is.

If you want to use a rendered template anywhere on the document use r before the string.

That is:

env.x = 12;
# normal string
"Some {x}"
# rendered template string
r"Some {x}"

Results:

"Some {x}"

"Some 12"