I am having trouble finding a solution to a problem when using the foreach
loop in Haxe Templates. I am using the following code to produce a HTML form from a given input:
public function execute(outputString:String)
{
var result = "";
var data:ElementData = { elements: new Array<String>() };
for (e in elements)
{
var value = types.get(e.id)(e);
data.elements.push(value);
}
for (e in data.elements)
trace(e);
var t = new Template(outputString);
result = t.execute(data);
return result;
}
Using this code, with the following input:
auto "ID"
text "Title"
I get the output from the trace calls above as:
Parser.hx:122: <input type="text" readonly="true" name="ID" />
Parser.hx:122: <input type="text" name="Title" />
I know from this that the input is correctly being parsed.
I use the following Main.hx
to get all this:
class Main
{
public static function main()
{
var p = new Parser("examples/simple/script.form");
p.types.set("auto", parse_auto);
p.types.set("text", parse_text);
p.parse();
var result = p.execute(sys.io.File.getContent("examples/simple/basic.html"));
Sys.stdout().writeString(result);
}
static function parse_auto(d:Element):String
{
var result = '<input type="text" readonly="true" name="${d.title}" />';
return result;
}
static function parse_text(d:Element):String
{
var result = '<input type="text" name="${d.title}" />';
return result;
}
}
And in basic.html
I have the following:
<!DOCTYPE html>
<html>
<body>
::foreach elements::
::__current__::
::end::
</body>
</html>
Just very simple, to get things working. However, when trying to use this file with the p.execute
call, I get the following error:
Called from ? line 1
Called from Main.hx line 16
Called from Parser.hx line 124
Called from C:\HaxeToolkit\haxe\std/haxe/Template.hx line 110
Called from C:\HaxeToolkit\haxe\std/haxe/Template.hx line 369
Called from C:\HaxeToolkit\haxe\std/haxe/Template.hx line 385
Called from C:\HaxeToolkit\haxe\std/haxe/Template.hx line 369
Called from C:\HaxeToolkit\haxe\std/haxe/Template.hx line 356
Called from C:\HaxeToolkit\haxe\std/haxe/Template.hx line 120
Called from C:\HaxeToolkit\haxe\std/neko/_std/Reflect.hx line 25
Uncaught exception - Invalid field access : __s
I don’t understand why this is happening, because the array elements
in the data
variable passed into the template is initialised, and I have double-checked that the array actually has data stored in there, as seen above. So my question is how can the Template system not pick this up?
Any help would be great!