You can use any template-engine you like, but tk_self is prepared for three template-engines: PHP, Smarty and PHPTAL; the engine to use is defined in the tk_settings-file:
/conf/tk_settings.php
The following examples show, how to use this template-engines to render the context of the view-function from the test.php file :
/views/test.php
The three template files are stored in:
/tmpl/php/test.tpl /tmpl/phptal/test.tpl /tmpl/smarty/templates/test.tpl
Refer to the chapter Installation for the directory layout.
Because PHP is a template language, it can be used very well as a kind of template-engine by inserting the prepared data (the context) into an html-file by using some short PHP processing instructions:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>tk_self test</title> <link rel="stylesheet" href="/static/tk_self/test.css" charset="utf-8" /> </head> <body> <h1>tk_self test</h1> <p>Template Engine: <b>PHP</b></p> <p>message: <?=$context["message"]?></p> <table> <tr> <th>Template Engines</th> <th>Homepage</th> </tr> <?php foreach ($context["engines"] as $engine => $address) { ?> <tr> <td><?=$engine?></td> <td><a href="http://<?=$address?>/"><?=$address?></a></td> </tr> <?php } ?> </table> </body> </html>
The advantage of using PHP as template engine is the independency from third party products and that this works really fast because there is no translation necessary: the template already is valid PHP-code. The disadvantage is, that it is easy to restart mixing presentation- and program-logic again. So if you use PHP for templates, use as few PHP processing instructions as possible.
Smarty removes PHP processing instructions from a template-file and adds a lot of functionality for presentation-logic. The code below shows how to write a template for Smarty:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>tk_self test</title> <link rel="stylesheet" href="/static/tk_self/test.css" charset="utf-8" /> </head> <body> <h1>tk_self test</h1> <p>Template Engine: <b>Smarty</b></p> <p>message: {$context.message}</p> <table> <tr> <th>Template Engines</th> <th>Homepage</th> </tr> {foreach from=$context.engines key=engine item=address} <tr> <td>{$engine}</td> <td><a href="http://{$address}/">{$address}</a></td> </tr> {/foreach} </table> </body> </html>
The PHP-processing instructions are gone, replaced by Smarty-processing instructions in curly brackets (which can be redefined in Smarty) and array-items accessed by a dotted-notation. To learn more about using Smarty, refer to the Smarty-Documentation.
PHPTAL implements the programming language independent Template Attribute Language (TAL, originally developed for Zope) in PHP. TAL defines additional namespaces (tal:, metal:) to insert processing instructions as HTML-attributes:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>tk_self test</title> <link rel="stylesheet" href="/static/tk_self/test.css" charset="utf-8" /> </head> <body> <h1>tk_self test</h1> <p>Template Engine: <b>PHPTAL</b></p> <p>message: <span tal:replace="context/message">text to be replaced</span></p> <table> <tr> <th>Template Engines</th> <th>Homepage</th> </tr> <tr tal:repeat="engine context/engines"> <td tal:content="repeat/engine/key"></td> <td><a href="http://${engine}/" tal:content="engine"/></td> </tr> </table> </body> </html>
TAL-templates have to be valid XML, produce valid XML and can be edited by Webdesigners with WYSIWYG-tools. To learn more, refer to the PHPTAL-Documentation.