Cyclone3 Skin

Template Toolkit

From Cyclone3 Wiki

(Difference between revisions)
Line 1: Line 1:
Template Toolkit is from commit r5950 (2011-09-07) integrated part of Cyclone3 Framework, special [[TOM::Template]] library and everyone can use it.
Template Toolkit is from commit r5950 (2011-09-07) integrated part of Cyclone3 Framework, special [[TOM::Template]] library and everyone can use it.
 +
 +
== Differences between Template Toolkit and Cyclone3 Template Toolkit ==
 +
 +
* Template Toolkit templates (tt2) are integrated in Template files (tpl)
 +
<pre>
 +
<?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>
 +
</pre>
 +
* Variable names with dots can be escaped
 +
$hash{'variable'}{'var.name'}=5
 +
[%variable.var\.name%]
== Examples of usage ==
== Examples of usage ==

Revision as of 09:34, 19 May 2012

Template Toolkit is from commit r5950 (2011-09-07) integrated part of Cyclone3 Framework, special TOM::Template library and everyone can use it.

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() || 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.

Module variables

[%USE dumper%]
[%dumper.dump(domain)%]
[%dumper.dump(request)%]
[%dumper.dump(user)%]
[%dumper.dump(module)%]