This is an old revision of the document!
index.php
:
<?php require_once "PHPTAL/PHPTAL.php" // name of template. You can set/change it later with setTemplate() $phptal = new PHPTAL("hello.xhtml"); // any field set on PHPTAL object is visible to templates as variable $phptal->who_to_greet = "World"; // execute() returns result of template echo $phptal->execute();
hello.xhtml
:
<p>Hello <em tal:content="who_to_greet"/>!</p>
Avoid passing PHPTAL object around your entire program. MVC programs should be independent of their view. Instead of setting variables directly on PHPTAL object, make your methods return all required information. You can also pass helper objects to PHPTAL which will load data on demand.
<?php $result_array = execute_my_page(); $result_array['logged_in_user'] = find_logged_in_user(); $result_array['sidebar'] = new SidebarHelper(); foreach($result_array as $k => $v) $phptal->set($k,$v);
<tal:block>
is an invisible element. You can use it whenever you want some TAL functionality, but don't want to litter code with unnecessary <div>
or <span>
.
main.xhtml
:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" metal:define-macro="layout"> <head> <title metal:define-slot="title"/> <tal:block metal:define-slot="other-stuff-in-head"/> </head> <body> <div id="main"><tal:block metal:define-slot="maincontent"/></div> </body> </html>
</html>
subpage.xhtml
:
<tal:block metal:use-macro="main.xhtml/layout"> <tal:block metal:fill-slot="title">This is title of the page!</tal:block> <p metal:fill-slot="maincontent">Hello world</p> </tal:block>