The idea to use macro's for the form / show results question is great. It seems like a valid alternative to adding the conditon statements.
I had gotten to this point.
search.php:
Code:
<?php
include_once( 'PHPTAL.php');
$tpl = new PHPTAL();
$tpl->setTemplateRepository('templates');
$tpl->setTemplate('search.tpl');
$tpl->title = "Test van PHPTal";
$tpl->globalpage = "global.tpl/globalpage";
function show_form() {
global $tpl;
$tpl->mode = 'showform';
}
function search() {
global $tpl;
$tpl->mode = 'showresult';
}
if (!isset($_REQUEST['submit'])) {
show_form();
}
else {
search();
}
echo $tpl->execute();
?>
Using 2 templates: a "general" one, that will be used by every PHP page and that sets the general layout of the site:
global.tpl
Code:
<html metal:define-macro="globalpage">
<head>
<title tal:content="title">Title here</title>
</head>
<body>
<table>
<tr>
<td valign="top">
<span metal:define-slot="pagemenu">
[ <a href="/">Home</a> ]
</span>
</td>
<td>
<span metal:define-slot="pagecontent"></span>
</td>
</tr>
</table>
</body>
</html>
and a specific one, that defines the screens for this PHP page
search.tpl
Code:
<tal:block metal:use-macro="${globalpage}">
<tal:block metal:fill-slot="pagecontent">
<tal:block tal:condition="php: mode=='showform'">
...show the form...
</tal:block>
<tal:block tal:condition="php: mode=='showresult'">
...show the results...
</tal:block>
</tal:block>
</tal:block>
Given your excellent view on the best way to implement PHPTal: what do you think? How could I improve this?
About the 3-D-array and patTemplate: it also depends on your PHP code. Although you don't need run through your array of results, store them in a second array and add that to the page afterwards. It seems a bit overhead, the way PHPTal forces you to do it. But I'm not claiming it's that much better in patTemplate. I just hoped there was a better way in PHPTal :)
FYI, the patTemplate way: let's say we have something
Code:
$persons_res = pg_query("select firstname, lastname from persons");
while ($person = pg_fetch_object($persons_res)) {
$tmpl->addObject('person', $person);
$tmpl->parseTemplate('person', 'a');
if ($i % $num_records_per_row == 0) {
$tmpl->parseTemplate('row', 'a');
$tmpl->clearTemplate('person');
}
}
Where the template looks like this:
Code:
<h1>All persons</h1>
<table>
<patTemplate:tmpl name="row">
<tr>
<patTemplate:tmpl name="person">
<td>{FIRSTNAME} {LASTNAME}</Td>
</patTemplate:tmpl>
</tr>
</patTemplate:tmpl>
My experience with patTemplate: it served me very well for different big projects, but it's a bit cumbersome (too much text) to add conditional viewing of certain buttons etc.