Prev | Current Page 490 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

This is possible
because the ::foreach:: structure creates a new context for each element in the loop. It is possible to
access to the context value using the special variable name __current__ . This is very handy when the
elements in the list are not objects but primitives like numbers or strings.
The ::foreach:: block, as the ::if:: block, must be closed using the ::end:: expression or an error
will be thrown.
The following example just uses a simple function to return an Iterator object that outputs the
numbers in the Fibonacci sequence up to the specified value as argument. Numbers whose values are
the sum of the two previous ones compose the Fibonacci sequence. In the template, the __current__
special variable must be used to access the numeric values.
class Main
{
static function main()
{
var template = ???::foreach numbers::::__current__:: ::end::???;
var t = new haxe.Template(template);
var out = t.execute({ numbers : fibonacci(300) });
trace(out);
}
static public function fibonacci(upto : Int)
{
var a = 0;
var b = 1;
return {
hasNext : function() {
return b < = upto;
},
next : function() {
var v = b;
(continued)
Part II: Server Side, JavaScript, and Flash; Oh My!
256
b = b + a;
a = v;
return v;
}
}
}
}
Importing Templates into Templates
A command to import a template into another template does not exist, but the same effect can be easily
obtained executing the first and passing the result into the container template:
class Main
{
static function main()
{
var contentTpl = new haxe.


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