TIP: E4X for HTML Templates
Posted: Tue Feb 11, 2020 5:45 pm
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.
The result is:
You can also do it dynamically in some loops:
The result is:
Have fun with these. Lots more that you can do with E4X.
- Code: Select all
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:
<html>
<body>
<table border="0">
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
</body>
</html>
You can also do it dynamically in some loops:
- Code: Select all
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:
<html>
<body>
<table border="0">
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>g</td>
<td>h</td>
</tr>
</table>
</body>
</html>
Have fun with these. Lots more that you can do with E4X.