Template Toolkit
From Cyclone3 Wiki
(Difference between revisions)
(→Example for usage in modules) |
(→user - user management data) |
||
Line 121: | Line 121: | ||
'IP' => '124.29.138.18', | 'IP' => '124.29.138.18', | ||
'autolog' => 'N', | 'autolog' => 'N', | ||
- | |||
'datetime_last_login' => '2012-09-03 16:03:00', | 'datetime_last_login' => '2012-09-03 16:03:00', | ||
'datetime_login' => '2012-09-03 16:03:00', | 'datetime_login' => '2012-09-03 16:03:00', | ||
Line 132: | Line 131: | ||
'logged' => 'N', | 'logged' => 'N', | ||
'login' => undef, | 'login' => undef, | ||
- | |||
'perm_roles_override' => undef, | 'perm_roles_override' => undef, | ||
'posix_owner' => 'tU7gxG4r', | 'posix_owner' => 'tU7gxG4r', | ||
'requests' => 2, | 'requests' => 2, | ||
'requests_all' => '469', | 'requests_all' => '469', | ||
- | |||
- | |||
'session' => { | 'session' => { | ||
'AB' => 'B', | 'AB' => 'B', | ||
Line 174: | Line 170: | ||
} | } | ||
}, | }, | ||
- | |||
'status' => 'Y', | 'status' => 'Y', | ||
'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:18.0) Gecko/18.0 Firefox/18.0' | 'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:18.0) Gecko/18.0 Firefox/18.0' |
Revision as of 13:04, 9 November 2012
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' => '0jfupIROFykM6oLAWnIwKxubmSxjN82o', 'ID_user' => 'tU7gxG4r', 'IP' => '124.29.138.18', 'autolog' => 'N', 'datetime_last_login' => '2012-09-03 16:03:00', 'datetime_login' => '2012-09-03 16:03:00', 'datetime_register' => '2012-07-27 10:26:38', 'datetime_request' => '2012-09-03 16:03:00', 'domain' => 'my.domain.com', 'email' => undef, 'email_verified' => 'N', 'hostname' => 'domain.com', 'logged' => 'N', 'login' => undef, 'perm_roles_override' => undef, 'posix_owner' => 'tU7gxG4r', 'requests' => 2, 'requests_all' => '469', 'session' => { 'AB' => 'B', 'USRM_G' => { 'ref_type' => 'direct', 'referer' => 'http://my.domain.com/recommended', 'time' => '1343377598', 'utm_campaign' => undef, 'utm_content' => undef, 'utm_medium' => 'direct', 'utm_source' => undef, 'utm_term' => undef }, 'USRM_S' => { 'ref_type' => 'direct', 'referer' => undef, 'time' => '1346680980' }, 'my_special_customer_vars' => { 'customer_email' => 'myke.sparr@host.com', 'customer_name' => 'Myke Sparr', 'customer_credit' => '1225', }, 'my_special_userdata' => { 'address' => ', ', 'address_city' => undef, 'address_street' => undef, 'address_zip' => undef, 'email' => , 'invoice_address_city' => undef, 'invoice_address_street' => undef, 'invoice_address_zip' => undef, 'name' => , 'phone' => } }, 'status' => 'Y', 'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:18.0) Gecko/18.0 Firefox/18.0' };
domain - domain information
Contains domain configuration.
- Sample content:
$VAR1 = { 'setup' => {}, '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', 'metadata' => ' <metatree> <section name="Others"/> </metatree> ', '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_keys' => '#json{"product.list.version":"computer_components","tpl":"components_grid"}', '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' } };
How to preview variable content
[%USE dumper%] [%dumper.dump(domain)%]