{repeat}
Sample Usage
{repeat: 2 + 2}{key: tab}{endrepeat}
Settings
Setting Name Type Description Positional formula The number of times to repeat or a list comprehension. locals text Optional identifier providing a list of form data in the command. General Command Settings trim yes/no/left/right If yes
whitespace is removed before and after the command. If left
, only whitespace to the left is removed. If right
, only whitespace to the right is removed.
Setting Name | Type | Description |
---|---|---|
Positional | formula | The number of times to repeat or a list comprehension. |
locals | text | Optional identifier providing a list of form data in the command. |
General Command Settings | ||
trim | yes/no/left/right | If yes whitespace is removed before and after the command. If left , only whitespace to the left is removed. If right , only whitespace to the right is removed. |
The {repeat}
command replicates a portion of a snippet multiple times. It repeats everything that is between the {repeat}
and {endrepeat}
commands.
This command takes a formula as the first positional setting that must evaluate to one of two things:
- A Number
- A List Comprehension
Also, see the {if} command for an additional command that helps you build dynamic snippets.
Repeating with List Comprehensions
List Comprehensions are a special formula syntax in Text Blaze that allow you to transform lists.
As the first positional setting in the {repeat}
command instead of placing a number you place a list comprehension.
{=x} squared is {=x^2}{endrepeat}
Cities: {formtext: name=cities; default=Miami,London,New York}
{repeat: for city in split(cities, ",")}
{=city} spelled backwards is {=reverse(city)}
{endrepeat: trim=left}
{repeat: for (color, i) in ["red", "green", "blue"] if color <> "green"}
Color {=i} is {=color}. {endrepeat}
Variable Scoping in the {repeat}
command
When using the list comprehensions with the {repeat}
command, a separate variable scope gets created.
This means that the value of variables within a {repeat}
may differ from variable values outside the repeat.
This is called variable scoping and the following rules summarize how this works:
- The value of variables in the repeat iterator (e.g.
x
in the iteratorfor x in [1, 2, 3]
) are only available inside of the command, between the{repeat}
and{endrepeat}
commands. If the variables are defined outside the block (e.g. you have a{formtext: name=x}
command outside the block), then the values will not be changed outside the block but will be overridden within the block. - If a variable is defined outside the repeat block, it can be used in formulas inside the block. Form fields that have the same name inside the block will all use and change the same global value.
- If a variable is not defined outside the repeat block, any form fields inside the block will each refer to a unique instance of that variable for each iteration of the
{repeat}
command.
The following example illustrates this scoping behavior:
{repeat: for name in ["Jack", "Bob"]} {=name} likes the color {formtext: name=color}. The color {formtext: name=color} is {= "not" if popular <> color else ""} the same as the most popular color: {formtext: name=popular}. {endrepeat}
Accessing scoped variables outside of the {repeat}
command
If you need access to the variables scoped to inside of the {repeat}
command, you can use the optional locals
named setting.
If used, the locals
setting will create a list for you, with the identifier provided, where each item in the list is a keyed list for each repetition the command does.
Each key in the keyed list will be the name of the variable and the value for that key will the value of the variable.
For example, if we had a time sheet for our employees and we wanted to sum up their hours worked. We could implement it something like this:
{repeat: count; locals=employees} Employee Name: {formtext: name=name} -- Hours worked: {formtext: name=hours; default=5} {endrepeat}
Total hours worked: {=sum(map(employees, employee -> employee.hours))}
Employees List: {=employees}
A note on whitespace
The {repeat}
command does not output anything, but it still takes up whitespace in your snippet.
The optional trim
setting is useful for removing whitespace around the {repeat}
and {endrepeat}
commands.
See the whitespace guide for more information on why this happens in Text Blaze.