Template Toolkit
From Cyclone3 Wiki
Template Toolkit is a part of Cyclone3 Framework's TOM::Template library.
Contents |
Differences between Template Toolkit and Cyclone3 Template Toolkit
- Template Toolkit templates (tt2) are integrated in Template files (tpl)
<?xml version="1.0" encoding="UTF-8"?> <template> <header> <L10n level="auto" name="l10n_name" lng="auto"/> <extend level="auto" name="template_name" /> <extract> <file location="graphic_file_name.png"/> </extract> </header> <entity id="main"><![CDATA[ ... [%title%] ... ]]></entity> </template>
- Variable names with dots can be escaped
$hash{'variable'}{'var.name'}=5 [%variable.var\.name%]
Examples of usage
For Template Toolkit grammar check this documentation [1]
Perl code
my $tpl=new TOM::Template( 'level' => "auto", 'name' => "test", 'content-type' => "xhtml" ); # fill test data for (1..10) { my %db0_line=( 'name'=>"test string".$_ ); $tpl->variables_push(\%db0_line); } $tpl->process(); print $tpl->{'output'};
XML template (TOM::Template)
<?xml version="1.0" encoding="UTF-8"?> <template> <header> <tt enabled="true" /> </header> <entity id="main"> <![CDATA[ <h1>[%title%]</h1> <ul> [%FOREACH item IN items%] <li>name: [%item.name%]</li> [%END%] </ul> ]]> </entity> </template>
Example for usage in modules
Calling module with tpl parameters:
<MODULE> <VAR id="-type" value="mdl" /> <VAR id="-addon" value="a401" /> <VAR id="-name" value="article_list" /> <VAR id="-version" value="tpl" /> <VAR id="-level" value="global" /> <VAR id="-tpl" value="default" /> <VAR id="-TMP" value="CONTENT" /> </MODULE>
Inside module
sub execute { my %env=@_; Tomahawk::GetTpl('env'=>\%env) || return undef; ... $TPL->variables_push(\%db0_line); ... return 1; }
GetTpl() loads file 401-article_list.tpl.default.tpl located in local _mdl directory. After module exit is processed tt in main entry of tpl.
Standard Cyclone3 template variables
user - user management data
Contains user management information, cookies and session data.
- Sample content
$VAR1 = { 'ID_session' => 'jrRKQp0jeoltH15AuXo4c2xhHbJfPACh', 'ID_user' => 'ukIVsPsW', 'IP' => '134.19.128.18', 'autolog' => 'N', 'datetime_last_login' => '2013-06-11 09:09:37', 'datetime_login' => '2013-06-11 09:09:37', 'datetime_register' => '2012-05-15 16:58:41', 'datetime_request' => '2013-06-11 09:33:48', 'domain' => 'my.domain.com', 'email' => 'myke.sparr@host.com', 'email_verified' => 'N', 'hostname' => 'domain.com', 'logged' => 'Y', 'login' => 'msparr', 'profile' => { 'ID' => '45750', 'ID_entity' => 'ukIVsPsW', 'PIN' => undef, 'ZIP' => undef, 'about_me' => undef, 'address_current' => undef, 'address_postal' => undef, 'bank_contact' => undef, 'birth_country_code' => undef, 'birth_place' => undef, 'city' => undef, 'country_code' => 'SK', 'county' => undef, 'date_birth' => '1979-09-01', 'datetime_create' => '2013-06-03 21:34:18', 'district' => undef, 'education' => undef, 'email_office' => undef, 'email_public' => undef, 'firstname' => 'Myke', 'gender' => 'M', 'idcard_num' => undef, 'lng' => 'xx', 'maidenname' => undef, 'metadata' => "<metatree> <section name=\"Registration\"> <variable name=\"name\">Myke Sparr</variable> <variable name=\"age\">30</variable> </section></metatree>", 'middlename' => undef, 'name_prefix' => undef, 'name_suffix' => undef, 'note' => "some plain text", 'passport_num' => undef, 'phone' => undef, 'phone_home' => undef, 'phone_mobile' => undef, 'phone_office' => undef, 'phys_height' => undef, 'phys_weight' => undef, 'posix_modified' => 'ukIVsPsW', 'rating_weight' => '0.13', 'state' => undef, 'status' => 'Y', 'street' => undef, 'street_num' => undef, 'surname' => "Sparr" }, 'requests' => 32, 'requests_all' => '2818', 'secure_hash' => 'IZVhijt9EgUTA6Ui', 'session' => { 'AB' => 'B', 'USRM_S' => { 'ref_type' => 'onsite', 'referer' => 'http://my.domain.com/recommended', 'time' => '1370432530' }, 'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:24.0) Gecko/20130610 Firefox/24.0' };
domain - domain information
Contains domain configuration.
- Sample content:
$VAR1 = { 'setup' => {}, 'name' => 'my.domain.com', 'url' => 'http://my.domain.com', 'url_a501' => 'http://my.domain.com/media/a501', 'url_a510' => 'http://my.domain.com/media/a510', 'url_css' => 'http://my.domain.com/media/css', 'url_grf' => 'http://my.domain.com/media/grf', 'url_js' => undef, 'url_master' => 'http://my.domain.com', 'url_media' => 'http://my.domain.com/media' };
request - request data
Contains current request information, form variables, sitemap information and sitemap key setup.
- Sample data:
$VAR1 = { 'a210' => { 'ID' => '143', 'ID_charindex' => '00B:000:000', 'ID_entity' => '143', 'IDs' => [ '138', '139', '143' ], 'alias_url' => , 'datetime_create' => '2012-09-03 15:34:51', 'is_default' => 'N', 'lng' => 'sk', 'name' => 'Internal', 'name_url' => 'internal', 'node' => $VAR1, 'nodes' => [ { 'ID' => '138', 'ID_charindex' => '00B', 'ID_entity' => '138', 'name' => "Components", 'name_url' => 'components', 'status' => 'Y' }, { 'ID' => '139', 'ID_charindex' => '00B:000', 'ID_entity' => '139', 'name' => "Harddrives", 'name_url' => 'harddrives', 'status' => 'Y' }, { 'ID' => '143', 'ID_charindex' => '00B:000:000', 'ID_entity' => '143', 'datetime_create' => '2012-09-03 15:34:51', 'lng' => 'sk', 'name' => 'Internal', 'name_url' => 'internal', 'status' => 'Y' } ], 'path_url' => 'components/harddrives/internal', 'status' => 'Y', 't_name' => 'product_list', 't_name_used' => 1, 'visible' => 'Y' }, 'code' => 'P79NKcxT', 'key' => { 'title' => "Internal computer drives" }, 'param' => { 'TID' => 'l_product_list', '__lng' => 'en', 'a210_path' => 'components/harddrives/internal', 'form_variable1' => 'somevalue', 'form_variable2' => 'someothervalue', 'type' => 'product_list' } };
module - current module data
Contains domain configuration variables, etc.
- Sample content:
$VAR1 = { 'env' => { '-addon': 'a210', '-name': 'page_tree', '-version': 'tpl', '-level': 'global', '-tpl': 'main_menu', '-TMP': 'HEADER-MENU', } };
Hints and howto's
Dump variable content
[%USE dumper%] [%dumper.dump(domain)%]
Escape html strings (security)
"[%item.name | html%]