You may find yourself frequently reusing a block of layout, such as a monster stat block, skill check, or shop. In this case you can use a template
to store the layout and reusse it.
For a lighter-weight alternative, take a look at placeholders
Templates are declared inside the gb-templates
passage. The passage can contain one or more <template>
tags. For example:
To use a template in a passage, use the <t>
tag:
This would outout whatever the content of the template test
is (in this case [CONTENT]
).
Given a template, 'dead':
And a passage content of:
You would see:
If you prefer you can use the syntax <template name="template_name"></template>
, or the placeholder shortcut $template_name
. In addition, for compatibility with the HTML Custom Element syntax, <gb-template>
is also valid.
For example, all of the following are equivalent:
A template with fixed text has limited uses (though not none). More often you will want to pass some content to a template. For example imagine you have a specially styled div in your game for notes, and want to use that frequently. You could set up a template and pass the content of the note.
In this template the text {{default}}
is going to be replaced with the default content of the template, which is everything between the <t>
and </t>
tags.
{{default}}
is actually a specical case variable, supplied when there are no other variables inside the data passed to the template. Instead of just passing some text, you can pass a JSON object of values.
You can access values from the object by putting the key in {{}}
as shown above. If your data has nested values (arrays, or other objects) you can access them with "dot notation", for example: {{weapon.damage}} {{weapons.0}}
.
You can do simple calculation inside a numerical values by following the value name with one of + - / *
. For example {{default + 1}}
.
You may wish to vary the layout of your template depending on the data passed — if X then Y, for example. You can do that using the <if>
tag inside your template.
You can use the following for comparisons:
== | equals |
>= | greater than or equals |
<= | less than or equals |
> | greater than |
< | less than |
!= | not equals |
The following logical operations are also available: or, and, not
. So this is valid:
One of the best features of a template is repeating layout. For example, you might lay out images in a gallery, or lines of information in a shop. You can do this using the <repeat>
tag. The simplest form of this repeats its contents a fixed number of times.
The number of repeats can come from a variable, as usual.
Alternatively you can loop over the contents of a variable doing something for each value.
In this sort of loop (<repeat ARRAY as VARIABLE>
) ARRAY
is the thing you are looping through and VARIABLE
is a name assigned to it.
Given a template, 'gallery':
And a passage content of:
You would see:
When working with <repeat>
some special variables are available.
{{last}}
is true when you are on the last repeat of the loop{{LOOPNAME_length}}
is a number indicating the length of the item you are repeating over{{LOOPNAME_index}}
is a number indicating the current repeat you are onLOOPNAME
is the name of the repeat, which is the same as the variable you are repeating over, with any dots replaced by underscores. For example <repeat bags as bag>
generates bags_length
and bags_index
. <repeat items.bags as bag>
generates items_bags_length
and items_bags_index
.
Data looks like:
Data looks like: