CompileStringTemplate Function


P Result = CompileStringTemplate(C string)



String template that has expressions enclosed in braces { }.



Returns an object with the following methods:


Evaluates the template and returns a character string with the merged data.


Compile a string template that has expression enclosed in { }. Returns an object. Use <obj>.Output() to evaluate the string.


Compile a string template that has expression enclosed in braces { }. The compiled template created is an object, which is returned from the function. This compiled object has an .Output() method that you can call to evaluate the string.

The following sequence is the equivalent of new_string = evaluate_string(template):

obj = compileStringTemplate(template)
new_string = obj.Output()

While this may look more complicated than an evaluate_string() call, compileStringTemplate() is much more efficient than evaluate_string() when used inside a loop.

What can go inside the opening and closing braces { } can be arbitrarily complex Xbasic expressions. In the examples below, we have restricted ourselves to database fields with simple character manipulation functions for simplicity.


In this example, the first row in the customer table has a firstname of Tom and a city of Boston. This example is written for a DBF table.

txt = "hello {t.data(\"firstname\")}: Welcome to {upper(t.data(\"city\"))}"
dim t as p
t = table.open("customer")
? evaluate_string(txt)
= "Hello Tom: Welcome to BOSTON.

Here is the same example using a compiled string template:

txt = "hello {t.data(\"firstname\")}: Welcome to {upper(t.data(\"city\"))}"
dim p as p 
p = compilestringtemplate(txt)
dim t as p
t = table.open("customer")
= "Hello Tom: Welcome to BOSTON.

'Note that <tbl>.data("fieldname") allows you to read data from a field in a table. 
'tbl.data("firstname"), is similar to the tbl.firstname, i.e. both return the value in the firstname
'field. But tbl.data("firstname") removes trailing blanks.

An extremely common pattern in Web Applications is to do a database query and then generate some HTML content to send to the browser. The HTML content is often generated by merging data from the database into an HTML template. The evaluate_string() is ideal for merging data into a template. In the case where the HTML content is constructed by looping over the records in the query and merging data into the template for each row in the query, the compileStringTemplate() function can be used to improve performance.

This example is written for AlphaDAO.

dim tableBegin as c 
tableBegin = "<table>"
dim thead as c 
thead = <<%html%
<th class="customerId">CustomerId</th>
<th class="CompanyName">Company Name</th>
<th class="ContactName">Contact Name</th>
<th class="Address">Address</th>
<th class="City">City</th>
<th class="Country">Country</th>

dim tbodyBegin as c 
tbodyBegin = "<tbody>"

dim tRow as c 
trow = <<%html%
<td class="gridCell">{a5_html_label(rs.data("customerId"))}</td>
<td class="gridCell">{a5_html_label(rs.data("companyName"))}</td>
<td class="gridCell">{a5_html_label(rs.data("contactName"))}</td>
<td class="gridCell">{a5_html_label(rs.data("address"))}</td>
<td class="gridCell">{a5_html_label(rs.data("city"))}</td>
<td class="gridCell">{a5_html_label(rs.data("country"))}</td>

dim tbodyEnd as c 
tbodyEnd = "</tbody>"

dim tableEnd as c 
tableEnd = "</table>"

dim cn as sql::connection
dim sql as c 
sql = "select customerId, CompanyName, ContactName, Address, City, Country from customers order by companyname"

dim rs as sql::resultSet
rs = cn.ResultSet

dim htmlRows as c = ""
dim p as p
'compiling the string template is faster than using the evaluate_string() function in a loop.
p = compileStringTemplate(trow)

while rs.nextRow()
    'the *concat() function is much faster than doing: htmlRows = htmlRows + p.output()
end while

dim allHTML as c 
allHTML = tableBegin +crlf()+\
thead + crlf()+\
tbodyBegin + crlf()+\
htmlRows + crlf()+\

'if you want to see how the html will render
'if you want to see the raw html


See Also