Subsections


4. Template variables


4.1 Using SET Statements

With the SET statement you can assign values to variables inside the templates. This allows the designer to store values he needs so he can access them more easy.

beispiel.tpl

{BEGIN BLOCK list}
    {IF list.FIRST = True}
        {IF OPENED_TAG = True}
</ul>
        {ENDIF}
<ul>
        {SET OPENED_TAG = True}
    {ENDIF}
    <li>{list.ITEM}</li>
{END BLOCK list}
</ul>

beispiel.py

from tplEngine import *
template = TplTemplate('beispiel', 'beispiel.tpl')

items = [('A1', True),('A2', False),('B1', True),('B2', False),('B3', False)]
for item in items:
    template.assign_block_vars('list', 
        {'ITEM': item[0],
         'FIRST': item[1]})
    
print template.get_executed()

Console

$ python beispiel.py

<ul>
    <li>A1</li>
    <li>A2</li>
</ul>
<ul>
    <li>B1</li>
    <li>B2</li>
    <li>B3</li>
</ul>

If SET had not been used there would have been a "</ul>" in the first line. Of course we could have done this differently but you will probably find cases where you really need this.

See About this document... for information on suggesting changes.