I was working on a client project and saw they were doing a lot of string concatenation for building some HTML. I converted it to E4X, but i noticed there weren’t many examples on the forum, so I thought I’d share a few very simple ones.
var someVar = 1
var someVar2 = 2
var template = <html>
<body>
<table border="0">
<tr>
<td>{someVar}</td>
<td>{someVar2}</td>
</tr>
</table>
</body>
</html>
var merged = template.toString()
application.output(merged)
The result is:
1 2
You can also do it dynamically in some loops:
var ds = databaseManager.createEmptyDataSet(0,2)
ds.addRow(['a','b'])
ds.addRow(['d','e'])
ds.addRow(['g','h'])
var template = <html>
<body>
<table border="0">
</table>
</body>
</html>
for(var i=1; i<=ds.getMaxRowIndex(); i++){
template.body.table.appendChild(<tr>
<td>{ds.getValue(i,1)}</td>
<td>{ds.getValue(i,2)}</td>
</tr>)
}
var merged = template.toString()
application.output(merged)
The result is:
a b d e g h
Have fun with these. Lots more that you can do with E4X.