| Introduction | Start | acdkcfgscript | Language | Library | Embedding | CfgScript IDE | Debugging | Templates | Samples | Wish List |
CfgScript provides a template language, quite similar to JSP.
In scripting language often text files are build
with static parts (which are not changed) and dynamic parts.
In CfgScript are multiple options to merge this static and
dynamic parts.
String head = "<html><header><title>Hello</title></header><body>";
String footer = "</body></html>";
StringArray hellos = [ "First", "Second", "Third" ];
StringBuffer sb = new StringBuffer();
sb.append(head);
sb.append("<table>");
foreach (String s in hellos)
{
sb.append("<tr><td>").append(s).append("</td></tr>");
}
sb.append("</table>");
sb.append(footer);
out.println(sb.toString());
|
This results in:
<html><header><title>Hello</title></header><body><table><tr><td>First</td></tr><tr><td>Second</td></tr><tr><td>Third</td></tr></table></body></html>
|
Of course this is very simple example. In normal cases the static part of the text is much more complex:
A second posibility is to use Backtick Expressions.
StringArray hellos = [ "First", "Second", "Third" ];
String s =
`"<html>
<header>
<title>Hello</title>
</header>
<body>
<table>
!{ foreach (String s in hellos) out.println(\"<tr><td>\" + s \"</td></tr>\"); }!
</table>
</body>
</html>"`;
out.println(s);
|
Output:
<html><header><title>Hello</title></header><body><table><tr><td>First</td></tr><tr><td>Second</td></tr><tr><td>Third</td></tr></table></body></html>
<html>
<header>
<title>Hello</title>
</header>
<body>
<table>
<tr><td>First</td></tr>
<tr><td>Second</td></tr>
<tr><td>Third</td></tr>
</table>
</body>
</html>
|
But for more complex cases this is not always available. Quoting inside the loop is quite complex.
CfgScript Templates are textfiles, in which CfgScript code can be embedded.
<html>
<header>
<title>Hello 3</title>
</header>
<body>
<table>
<@ foreach (String s in sa) {
@> <tr><td><@= s @></td></tr>
<@ } @></table>
</body>
</html>
|
To evalute this such a template you can use following code (CfgScript or C++):
// get name of current script, get parent directory
File dir = (new File(__script.getFileName())).getParentFile();
// look for all files ends with .cst in this directory
foreach (File f in dir.listFiles(new GlobFilenameFilter("*.cst")))
{
// create a new script instance
Script s = new Script(f.getPath());
// execute template.
/*
Because in __props (which represends all variables the current scope)
out is defined, the template writes to out.
*/
StringArray sa = [ "First", "Second", "Third" ];
s.evalTemplate(f, __props);
}
|
The Script::evalTemplate() function read the given file
and write it to the writer defined in the variable out of the
given Props.
The regions marked with <% %> will be evaluated as Script Code.
The variables defined in Props given to evalTemplate are also available in this
Script Code.
All text written to out in these section also written to out.
There is no limitation in CfgScript code in the <% %> section.
It can also contains includes, class definitions, etc.
The regions marked with <%= %> will be evaluated as CfgScript expression.
To the result of the expession toString() will applied and written to out
The regions marked with <!= %> are comments and will not appear in the output.
As shown in the sample the CfgScript loops can also contains template text. It will be
written to out every time the loop will be executed.
|