The first
foreach example can be rewritten in the following way:
< ul >
< li ::repeat p persons:: >
::p.name:: is ::p.age:: years old
< /li >
< /ul >
set, fill, and use Expressions
The set expression permits creating variables inside a template. Using template variables can be handy
in many situations; it is possible to accumulate the values from a list in a sum, or to calculate a value
once and print it several times on the same page.
??‘
??‘
??‘
??‘
??‘
??‘
??‘
Chapter 10: Separating Design Using Templates
265
The set syntax is as follows:
::set varname = varvalue::
Where varname is an arbitrary name used to store varvalue that is a standard Templo expression. Note
that the set expression does not print anything; it is necessary to use a standard print expression to
display the value of the newly created variable. To sum the values from a cycle it is possible to write a
template like this:
::set sum = 0::
::foreach num numbers::
::set sum += num::
::end::
The sum total is: ::sum::
The fill expression works in a very similar way, but instead of capturing the value from an expression,
it will capture a fragment of template.
Pages:
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521