Cyclone3 Skin

Template Toolkit

From Cyclone3 Wiki

(Difference between revisions)
(Examples of usage)
(Example for usage in modules)
Line 82: Line 82:
</pre>
</pre>
-
GetTpl() loads file '''401-article_list.tpl.default.tpl''' located in local _mdl directory.
+
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.
[[Category:Labs]]
[[Category:Labs]]

Revision as of 16:02, 20 October 2011

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

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.