4. Automatic variables and blocks
Automatic variables and blocks are filled automatically without any values specified in the input data. The automatic elements typically help with the formatting of the generated content.
4.1. Automatic left-alignment variable
A left-alignment automatic variable has a <+> tag and can be used to maintain the
left-alignment of a text within the template having variables filled
with values of different character lengths.
Blockie automatically scans the first character located right after this tag and counts the number of consecutive occurrences of this character until a different character is found. Then it maintains the column position of the different character regardless of the content generated on the line before this character.
template = """
Name Surname Role
----------------------------
<CHARACTERS>
<NAME><+> <SNAME><+> <ROLE>
</CHARACTERS>"""
blk = blockie.Block(template)
blk.fill({"characters": [
{"name": "Dave", "sname": "Bowman", "role": "astronaut 1"},
{"name": "Frank", "sname": "Poole", "role": "astronaut 2"},
{"name": "Heywood", "sname": "Floyd", "role": "chairman of the US National Council of Astronautics"},
{"name": "HAL", "sname": "9000", "role": "broken computer that can kill, but can't lie"}]})
print(blk.content)
Output:
Name Surname Role
----------------------------
Dave Bowman astronaut 1
Frank Poole astronaut 2
Heywood Floyd chairman of the US National Council of Astronautics
HAL 9000 broken computer that can kill, but can't lie
An example illustrating the use of non-space characters for the left-alignment:
template = """
Name Phone number
----------------------------
<PEOPLE>
<NAME><+>.......<PHONE>
</PEOPLE>"""
blk = blockie.Block(template)
blk.fill({"people": [
{"name": "Dave", "phone": "0940 123 456"},
{"name": "Frank", "phone": "0933 987 654"},
{"name": "Heywood", "phone": "0911 111 111"}]})
print(blk.content)
Output:
Name Phone number
----------------------------
Dave............0940 123 456
Frank...........0933 987 654
Heywood.........0911 111 111
4.2. Automatic block variable
A block content can be set into a different location than its original
location, or into multiple different locations within the template using a special tag having a
<@BLOCK_NAME> format. This so-called block variable acts as a target for a block content,
while the original block is automatically cleared.
The block variable can be useful for repeating the referenced content multiple times or for maintaining the left-alignment as illustrated in the example below.
template = """
<LIST>
<IDX>a)<^IDX>b)<^IDX>c)<^IDX>d)<^IDX>e)<^IDX>f)</IDX>
<@IDX> <ITEM><+> <QTY>
</LIST>
"""
blk = blockie.Block(template)
blk.fill({"list": [
{"idx": 0, "item": "first", "qty": 1},
{"idx": 1, "item": "second", "qty": 2},
{"idx": 2, "item": "third", "qty": 3}]})
print(blk.content)
Output:
a) first 1
b) second 2
c) third 3
Note
The block variable tag(s) must be located within the parent block of a referenced block.
4.3. Automatic variation block
With cloned block contents it is often useful to have some part of the
block content to be different in the first and/or the last clone. An automatic variation block
can be used to define such part of the content using a special <.> block tag having either two
or three content variations:
<.>standard content<^.>last content</.><.>standard content<^.>last content<^.>first content</.>
Where the standard content is used in the second and second to last clones of a parent block,
the last content is used in the last clone, and the first content is used in the first
parent block content.
blk = blockie.Block("Characters: <CHARACTERS><NAME> <SURNAME><.>, <^.></.></CHARACTERS>.")
blk.fill({"characters": [
{"name": "Dave", "surname": "Bowman"},
{"name": "Frank", "surname": "Poole"},
{"name": "Heywood", "surname": "Floyd"},
{"name": "HAL", "surname": "9000"}]})
print(blk.content)
Output:
Characters: Dave Bowman, Frank Poole, Heywood Floyd, HAL 9000.
template = """
<CHARACTERS>
<.>
| <NAME><+> <SURNAME><+> |
<^.>
| <NAME><+> <SURNAME><+> |
+----------------------------+
<^.>
+----------------------------+
| <NAME><+> <SURNAME><+> |
</.>
</CHARACTERS>"""
blk = blockie.Block(template)
blk.fill({"characters": [
{"name": "Dave", "surname": "Bowman"},
{"name": "Frank", "surname": "Poole"},
{"name": "Heywood", "surname": "Floyd"},
{"name": "HAL", "surname": "9000"}]})
print(blk.content)
Output:
+----------------------------+
| Dave Bowman |
| Frank Poole |
| Heywood Floyd |
| HAL 9000 |
+----------------------------+