Python interface to CTPP2 library.
Project description
Example of usage
First you should make template, file hello.tmpl:
Foo: <TMPL_var foo> <TMPL_if array> Here is loop body: <TMPL_loop array> Key: <TMPL_var key> </TMPL_loop> </TMPL_if>Now create Python script:
#!/usr/bin/env python import pyctpp2 if __name__ == '__main__': engine = pyctpp2.Engine() template = engine.parse('hello.tmpl') result = template.render(foo='bar', array=[{'key': 'first'}, {'key': 'second'}])Now check output:
Foo: bar Here is loop body: Key: first Key: second
CTPP2 built-in functions
There are a variety of situations when you need to represent data according to some condition. To simplify the solution of this problem CTPP2 support built-in functions. You can call them from the bodies of <TMPL_if, <TMPL_unless, <TMPL_var and <TMPL_udf operators.
Example 1:
<TMPL_var HTMLESCAPE(name)> <TMPL_if IN_SET(name, 1, 2, 3)> Variable "name" is set to "1", "2" or "3". </TMPL_if>CTPP2 support following built-in functions:
AVG
BASE64_ENCODE
BASE64_DECODE
CAST
DATE_FORMAT
DEFAULT
DEFINED
FORM_PARAM
GETTEXT, (_)
HMAC_MD5
HREF_PARAM
HTMLESCAPE
ICONV
IN_SET
JSESCAPE
JSON
MD5
MAX
MB_SIZE
MB_SUBSTR
MB_TRUNCATE
MIN
NUM_FORMAT
OBJ_DUMP
RANDOM
SIZE
SUBSTR
TRUNCATE
URIESCAPE
URLESCAPE
VERSION
XMLESCAPE
WMLESCAPE
AVG
Function calculates average value of arguments.
Arguments:
- 1:
Type of used algorithm for calculation of average value. Admissible values: ‘a’ (arithmetic), ‘g’ (geometric), ‘h’ (harmonic), ‘q’ (quadratic).
- 2..*:
Values.
Examples:
<TMPL_var AVG('a', 1, 2, 3)>: 2 <TMPL_var AVG('g', 1, 2, 3)>: 1.81712059283 <TMPL_var AVG('h', 1, 2, 3)>: 1.63636363636 <TMPL_var AVG('q', 1, 2, 3)>: 2.16024689947
BASE64_ENCODE
Function codes the value in format Base64 (RFC 3548).
Arguments:
- 1:
Value.
Examples:
<TMPL_var BASE64_ENCODE("Hello, World!")>
BASE64_DECODE
Function decodes the value from format Base64 (RFC 3548).
Arguments:
- 1:
Base64 string.
Examples:
<TMPL_var BASE64_DECODE("SGVsbG8sIFdvcmxkIQ==")>
CAST
Function can be used for conversion between types.
Arguments:
- 1:
Name of target type. Admissible values: “i[nteger]”, “o[ctal]”, “d[ecimal]”, “h[exadecimal]”, “f[loat]”, “s[tring]”.
- 2:
Value.
Examples:
<TMPL_var CAST("int", 1.345)>: 1 <TMPL_var CAST('int', "010")>: 8 <TMPL_var CAST('dec', "010")>: 10 <TMPL_var CAST('oct', "010")>: 8 <TMPL_var CAST('hex', "010")>: 16 <TMPL_var CAST("float", var1)>
CONCAT
Function concatenates arguments.
Arguments:
- 1..*:
Values.
Examples:
<TMPL_var CONCAT('a', 1, 2, 3)>: a123
DATE_FORMAT
Function formats the date according to a template. Syntax of a template completely matches with syntax for C-function strftime.
Arguments:
- 1:
Number of seconds since the Epoch.
- 2:
strftime template.
Examples:
<TMPL_var DATE_FORMAT(1200490323, "%Y-%m-%d %H:%M:%S")> <TMPL_udf DATE_FORMAT(1200490323, "%Y-%m-%d %H:%M:%S")>
DEFAULT
Function returns value of the second arguemtn in case the first isn’t set.
Arguments:
- 1:
Value.
- 2:
Default value.
Examples:
<TMPL_var DEFAULT(foo, "bar")>
DEFINED
Function returns true if the variable has the type which is distinct from UNDEF.
Arguments:
- 1:
Value.
Examples:
<TMPL_if DEFINED(foo)>Foo defined!</TMPL_if>
FORM_PARAM
The algorithm of function is similar HREF_PARAM. It is intended for a output in forms of fields of type hidden. Replaces with itself:
<TMPL_if a> <input type="hidden" name="param_a" value=<TMPL_var URLESCAPE(a)"> </TMPL_if>Arguments:
- 1:
Name of parameter.
- 2:
Value wrapped up in URLESCAPE.
Examples:
<TMPL_udf FORM_PARAM("param_a", a)>
GETTEXT, (_)
Function realizes system NLS support (Native Language Support, l18n).
Arguments:
- 1:
Value.
Examples:
<TMPL_var GETTEXT(variable)> <TMPL_var _(variable)>
HMAC_MD5
Function generates HMAC_MD5 hash from arguments.
Arguments:
- 1..*:
Values.
Examples:
<TMPL_var HMAC_MD5("Data", "key")>
HREF_PARAM
The algorithm of function is similar FORM_PARAM. It is intended for a output in links. Replaces with itself:
<TMPL_if a>param_a=<TMPL_var URLESCAPE(a)></TMPL_if>Arguments:
- 1:
Name of parameter.
- 2:
Value.
Examples:
<a href=/abc?<TMPL_udf HREF_PARAM("param_a", a)>
HTMLESCAPE
Function replaces symbols <, >, ", ', & on <, >, ", #039;, & accordingly.
Arguments:
- 1:
Value.
Examples:
<TMPL_var HTMLESCAPE(name)> <TMPL_udf HTMLESCAPE(name)>
ICONV
Function converts text from one encoding to another encoding.
Arguments:
- 1:
Encoding of the input.
- 2:
Encoding of the output.
- 3:
String value.
- 4:
Flags. Admissible values: ‘i’, ‘t’. Flag ‘i’ permits to convert string with errors. Flag ‘t’ enables transliteration.
Examples:
<TMPL_var ICONV("Windows-1251", "utf-8", "Здравствуй, мир!")> <TMPL_var ICONV("utf-8", "utf-8", "Здравствуй, мир!", "ti")>
IN_SET
Function compares the first argument to other arguments. Returns true if it is found though one coincidence.
Arguments:
- 1..*:
Values.
Examples:
<TMPL_if IN_SET(variable, "1", "2", "3")> <TMPL_if IN_SET(variable, variable1, "2", variable2)>
JSESCAPE
Function escapes symbols according to agreements of language ECMAScript (Java Script).
Arguments:
- 1:
Value.
Examples:
<TMPL_var JSESCAPE(foo)>
JSON
Function serializes object in format JSON.
Arguments:
- 1:
Value.
Examples:
<TMPL_var JSON(foo)>
LOG
Function calculates value of a logarithm of number. If the base not specified, returns the natural logarithm (base e).
Arguments:
- 1:
Number.
- 2:
Base.
Examples:
<TMPL_var LOG(2.7182818284)> <TMPL_udf LOG(100, 10)>
MD5
Function generates MD5 hash from arguments.
Arguments:
- 1..*:
Values.
Examples:
<TMPL_var MD5("Hello, World!")> <TMPL_var MD5("Hello", ", ", "World!")>
MAX
Function calculates the maximum value of arguments.
Arguments:
- 1:
Value.
- 2..*:
Values.
Examples:
<TMPL_var MAX(1, -2, 3)>: 3
MB_SIZE
Function returns the size of object. It returns length for arrays and dicts, count of characters for strings.
Arguments:
- 1:
String, array or dict value.
Examples:
<TMPL_var MB_SIZE(foo)>
MB_SUBSTR
Function is intended for gaining multibyte (UTF-8) substring or replacement of a part of a line.
Arguments:
- 1:
Input string.
- 2:
Start positiion.
- 3:
Count of characters.
- 4:
String of replacement.
Examples:
<TMPL_var SUBSTR('foobar', 2)>: oobar <TMPL_var SUBSTR('foobar', 2, 3)>: oba <TMPL_var SUBSTR('foobar', 2, 3, '1234')>: fo1234r
MB_TRUNCATE
Function truncates and output multibyte (UTF-8) lines.
Arguments:
- 1:
Input string.
- 2:
Count of characters.
- 3:
Tail string.
Examples:
<TMPL_var TRUNCATE('foobar', 3)>: foo <TMPL_var TRUNCATE('foobar', 3, '...')>: foo... <TMPL_var TRUNCATE('foobar', 100, '...')>: foobar
MIN
Function calculates the minimum value of arguments.
Arguments:
- 1:
Value.
- 2..*:
Values.
Examples:
<TMPL_var MIN(1, -2, 3)>: -2
NUM_FORMAT
Function formats integer and adds period sign.
Arguments:
- 1:
Integer value.
- 2:
Period sign.
Examples:
<TMPL_var NUM_FORMAT(variable, ",")> <TMPL_udf NUM_FORMAT(variable, ".")>
OBJ_DUMP
Function outputs dump of variables. If functions is called without arguments, then it returns dump of all variables.
Arguments:
- 1..*:
Variables.
Examples:
<TMPL_var OBJ_DUMP()> <TMPL_var OBJ_DUMP(var1, var2, var3)>
RANDOM
Function generates pseudorandom number. It returns value from range [0, RAND_MAX] without arguments. It returns value from range [0, argument] with 1 argument.
Arguments:
- 1:
First value.
- 2:
Second value.
Examples:
<TMPL_var RANDOM()> <TMPL_udf RANDOM(10)> <TMPL_udf RANDOM(1.5, 2.5)>
SIZE
Function returns the size of object. It returns length for arrays and dicts, count of bytes for strings.
Arguments:
- 1:
String, array or dict value.
Examples:
<TMPL_var MB_SIZE(foo)>
SUBSTR
Function is intended for gaining substring or replacement of a part of a line.
Arguments:
- 1:
Input string.
- 2:
Start positiion.
- 3:
Count of characters.
- 4:
String of replacement.
Examples:
<TMPL_var SUBSTR('foobar', 2)>: oobar <TMPL_var SUBSTR('foobar', 2, 3)>: oba <TMPL_var SUBSTR('foobar', 2, 3, '1234')>: fo1234r
TRUNCATE
Function truncates and output lines.
Arguments:
- 1:
Input string.
- 2:
Count of characters.
- 3:
Tail string.
Examples:
<TMPL_var TRUNCATE('foobar', 3)>: foo <TMPL_var TRUNCATE('foobar', 3, '...')>: foo... <TMPL_var TRUNCATE('foobar', 100, '...')>: foobar
URIESCAPE
Function is completely similar to function URLESCAPE except that the blank symbol is coded not as “+”, and as %20.
Arguments:
- 1:
Value.
Examples:
<TMPL_var URIESCAPE(name)> <TMPL_udf URIESCAPE(name)>
URLESCAPE
Function replaces symbols %XX, where XX - a hexadecimal code of a symbol.
Arguments:
- 1:
Value.
Examples:
<TMPL_var URLESCAPE(name)> <TMPL_udf URLESCAPE(name)>
VERSION
Function returns current versions of standard library CTPP2 and the virtual machine. Function returns the expanded output with the argument “full”.
Arguments:
- 1:
Admissible value: “full”.
Examples:
<TMPL_var VERSION()> <TMPL_var VERSION("full")>
XMLESCAPE
Function replaces symbols <, >, ", ', & on <, >, ", ', & accordingly.
Arguments:
- 1:
Value.
Examples:
<TMPL_var XMLESCAPE(name)> <TMPL_udf XMLESCAPE(name)>
WMLESCAPE
Function replaces symbols <, >, ", ', $, & on <, >, ", ', $$, & accordingly.
Arguments:
- 1:
Value.
Examples:
<TMPL_var WMLESCAPE(name)> <TMPL_udf WMLESCAPE(name)>
CHANGES
0.9.8 (12.05.2011)
Fixed extract method ‘ctpp2’ for Babel.
0.9.7 (28.04.2011)
Fixed support of render params with types.LongType.
0.9.6 (19.04.2011)
Fixed extract method ‘ctpp2’ for Babel.
0.9.5 (19.04.2011)
Fixed build with Cython.
0.9.4 (19.04.2011)
Added extract method ‘ctpp2’ for Babel.
0.9.3 (15.04.2011)
Added support of i18n.
Fixed tests.
Changed Template and Engine API.
0.9.2 (28.03.2011)
Fixed installation by pip.
0.9.1 (13.01.2011)
Added COPYING.txt.
Added support of traversable objects.
Some bug fixes.
0.9.0 (11.01.2011)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.