Subsections


7. TplEngine


7.1 Engine vs. Template

The TplEngine class provides a way to handle all templates through one object. It has several additional functions described in this chapter. The assign_vars() functions take an additional first argument which is called the file_id. For registering a new file you have to use register_file(file_id, file_name). Although you see the global Engine object has some advantages the Template object is often more handy in Python because you don't need to use the same object everywhere.


7.2 Default Variables

You cann assign default variables to the TplEngine Object. These variables are automatically assigned to all templates registered with the object.

__init__(defaults, recompile)
You can assign default variables with the constructor parameter defaults, recompile works like the TplTemplate constructor parameter.
get_defaults()
Returns a dict containing all registered default variables.
set_defaults(vars)
Sets the default variables dict to vars.


7.3 Templates From Strings

With the engine you can also use strings instead of template files. You have to use register_string(string_id, string) to do so. Afterwards you can handle the string as if it was a template file.


7.4 Highlighting

The TplEngine Class provides several methods to highlight strings inside of html code. This can be useful if you want to highlight a word the user searched for. Be aware that you shouldn't highlight a string twice.

add_highlight_string(string)
Adds a string which shall be highlighted.
add_highlight_strings(strings)
Adds multiple strings which shall be highlighted.
remove_highlight_string(string)
Removes a string which shall not be highlighted.
remove_highlight_strings(strings)
Removes multiple strings which shall not be highlighted.
set_highlight_replacement(string)
Overwrites the ReplacePattern from general.cfg which is used to highlight the specified strings.
highlight(self, html)
Highlights all strings in the code specified by html using ReplacePattern. Strings inside html tags and between <style>, <script> and </style>, </script> is not highlighted.

See About this document... for information on suggesting changes.