Since I was struggling with this too, I'm very interested in your

I use a different approach:

since a PHP-driven website usually consists of many .php scripts, I
like to keep my templates organised in a
1-template-for-each-php-script (eg. index.php uses index.tpl,
subscribe.php uses subscribe.tpl). Otherwise you're more busy with
opening and closing templates to make changes (and trying to remember
which file you need) than making the changes.

I have 1 file (general.tpl) that holds the general page structure of
every page, and then the 1-template-per-script as described above.

my general.tpl has something like (more complex than the example below
of course)

<html metal:define-macro="globalpage">
<div id="header" tal:define-slot="header"></div>
<div id="content" tal:define-slot="content"></div>
<div id="footer" tal:define-slot="footer"></div>

Every script then calls it's own template file (eg index.tpl)

<tal:block metal:use-macro="general.tpl/globalpage">
<tal:block metal:fill-slot="content">

Content of page goes here...


If I have a page that needs to show different pages (eg: form +
feedback after submission + error if required), eg register.tpl

<tal:block metal:use-macro="general.tpl/globalpage">
<tal:block metal:fill-slot="content" tal:condition="php:

<form method="post">
username: <input type="text" name="username"/>
Pwd: <input type="password" name="pwd"/>
<input type="submit" name="submit" value="Register"/>


<tal:block metal:fill-slot="content" tal:condition="php:

your registration was successfull


<tal:block metal:fill-slot="content" tal:condition="php:

your registration was not successfull. please try again.


register.php would then set $phptal->template = 'succeed' etc to show
the correct template...

In these circumstances, using AJAX to retrieve only part of the page
is harder. Do you have any feedback on that? For maintenance and
support, I really really like to keep my templates grouped with the
script. I know I could create a "register_form.tpl",
"register_succeed.tpl", "register_failed.tpl" etc but this quickly
becomes a cumbersome operation to open/close all those docs, let alone
if you have a PHP script that runs through multiple, consequetive

> On 18.09.2009, at 23:34, wrote:

>> Hi
>> I am working on ajax php website. Ajax makes using phptal more  
>> complicated.
>> My approach is to divide single page to several php files and every  
>> time at single page i create new object of $tpl. It means that I  
>> have to use several $tpl->execute() at the first page load. I think  
>> it is not optimal.
>> My question is whether there is most efective way to use phptal and  
>> ajax?

> page.xhtml:

> <body>
> stuff
> <tal:block metal:use-macro="widget.xhtml/show_widget" />
> stuff
> </body>

> widget.xhtml:

> <div metal:define-macro="show_widget">
> ajax me!
> </div>
> <tal:block metal:use-macro="show_widget" />

> This will let you either execute page.xhtml with all stuff in it, or  
> execute widget.xhtml and just output that single fragment. You can use
> variables in use-macro (e.g. "show_${name}"), so you're not limited to
> one thing per file.

PHPTAL mailing list

Reply via email to