7.6. Integration of templating engines
For those who are familiar with templating engines, Karrigell makes their integration very straightforward. Three engines are available though built-in functions, the others can be used like in normal Python scripts
7.6.1 Python string substitution
WARNING: PythonStringSubst has been superceded by KT, and will be dropped from the next release of Karrigell
This templating system uses the string substitution syntax that was introduced in
Python 2.4. Template files use placeholders of the form $foo
To use it in scripts, use the built-in function
PythonStringSubst(url,arg1=val1,arg2=val2...)
:
it will get the source string from the file at the specified url, and apply the keyword
arguments to this source string
For instance, suppose the template source is
<HTML> <HEAD><TITLE>$title</TITLE></HEAD> <BODY> $contents </BODY> </HTML>
print PythonStringSubst(src_url, title='Python String Substitution', contents='Hello World example' )
will be :
<HTML> <HEAD><TITLE>Python String Substitution</TITLE></HEAD> <BODY> Hello World example </BODY> </HTML>
7.6.2 KT - Karrigell Templates
The built-in KT function supercedes PythonStringSubst. In addition to string substitution, KT provides a mechanism for including other templates and specifying translations strings, which are passed to the Karrigell translation engine. KT is fully documented in this page
KT templates are stored in text files with .kt
extension. Conversion of existing
PythonStringSubst templates is easy:
- Change the extension of existing PythonStringSubst template files to
.kt
- Replace
"PythonStringSubst"
with"KT"
in Python and ks scripts.
7.6.3 Cheetah
If the Cheetah templating engine is available, you can use it the same way as
above, with a built-in function Cheetah(url,arg1=val1,arg2=val2...)
.
url
is the template url, and the keyword arguments are used to produce
the resulting HTML code
7.6.4 Other engines
To use another engine, you must apply its syntax in the script. Though the implementation details may vary, your code will probably look like this :
import Template # or raise Exception # get template source from file templateDef = open(template_file_name).read() # apply keywords to the template definition and print the result print Template(templateDef,arg1=val1,arg2=val2, ...)