Prev | Current Page 489 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


::if (condition)::
text to display when condition is true
::elseif (other-condition)::
text to display when other-condition is true
::else::
alternative text
::end::
Rounded parentheses are optional when the condition is expressed using just a variable name but they
add clarity to the code.
Loop Expressions
To loop in a sequence, an object that implements the Iterable or Iterator type, the foreach syntax is
used. The syntax is better explained using an example:
class Main
{
static function main()
{
var htmlFragment = ??? < ul > ::foreach countries::
< li > ::name:: (::population::) < /li > ::end:: < /ul > ???;
Chapter 10: Separating Design Using Templates
255
var list = [
{
name : ???United States??™,
population: ???~298M??™
}, {
name : ???Italy??™,
population: ???~58M??™
}, {
name : ???France??™,
population: ???~63M??™
}
];
var t = new haxe.Template(htmlFragment);
var out = t.execute({ countries : list });
trace(out);
}
}
// the result: is Main.hx:20: < ul > < li > United States
(~298M) < /li > < li > Italy (~58M) < /li > < li > France (~63M) < /li > < /ul >
In the preceding example, the elements of the data array are objects themselves; inside the ::foreach::
portion of the template the field names of the element objects are used to display values.


Pages:
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501