Warning! This page has the following tags:
- This page is deprecated as of Agavi 0.11.0, but still contains useful information.
- This page needs to be fixed and then merged with docbook documentation.
How to use the PHPTALView
This tutorial is not to teach you how you can build an application with agavi. We recommend you have read at least the QuickStart? guide.
The article will show you how to use the PHPTAL template engine with agavi. PHPTAL itself is a port of the "Zope Page Templates" in PHP. The only drawback you have, besided another dependency in your application, is that you have to produce clean XHTML templates.
How PHPTAL ticks
Here we have a sample TAL-template to see how a template with PHPTAL could look like. Don't be confused...you will get it later ;)
<div id="item" tal:repeat="value values"> <div id="item"> <span tal:condition="value/hasDate" tal:replace="value/getDate"/> <a tal:attributes="href value/getUrl" tal:content="value/getTitle"/> </div> <div id="content" tal:content="value/getContent"/> </div>
Installing PHPTAL
First we need to install PHPTAL. At the moment, we need the testing version of it cause since the last official release, there are several things we need to use it with agavi.
So let's install it via PEAR
pear install http://phptal.motion-twin.com/testing.tar.gz
First Example
Now, create a sample application and switch to your IndexSuccessView.class.php. We extend the View instead of eg. PHPView with PHPTALView, set your template and set a new attribute.
IndexSuccessView.class.php
<?php class Default_IndexSuccessView extends PHPTALView { /** * Execute any presentation logic and set template attributes. * * @return void * */ public function execute () { // set our template $this->setTemplate('IndexSuccess.tal'); // set the title $this->setAttribute('title', 'Index Action'); } } ?>
Now we can create our IndexSuccess.tal template in our module/templates directory. This could look like this:
IndexSuccess.tal
<h1 tal:content="title">This content will be replaced by the title!</h1>
Let's run the app and whats happening? Right...we see a big black "Index Action". Great :)
If you don't want that your attributes are direct accessible then just overwrite the extractAttributes() method of the view. Just insert like this:
public function extractAttributes()
{
return false;
}
Now you can access your attributes with e.g. "template/title" instead of "title".
PHPTAL and the decorator
Now lets ho a bit further and look how to use PHPTAL with the decorator?. We could extend our first sample view the the decorator initialisation like this:
public function execute ()
{
// set the decorator
$this->setDecoratorTemplate('master.tal');
// set a new slot
$this->setSlot('menu', 'Default', 'Menu');
// set our template
$this->setTemplate('IndexSuccess.tal');
// set the title
$this->setAttribute('title', 'Index Action');
}
Create a new action called Menu and fill the template with some content. Then lets create your master.tal in the templates directory. Our decorator could look something like this:
<html> <div tal:content="structure menu" style="border:1px solid red;"> Here will be our menu ! </div> <div tal:content="structure content" style="border:1px solid black;"> Here will be our content ! </div> </html>
We need the keyword structure in this case because PHPTAL automatically escapes the assigned template variables.
Now you can call your project in your browser and you'll see yout master template with the menu slot and your action content. Sweeet :)
I'm sorry to stop this tutorial here. I hope you'll find the "right way" to PHPTAL because this engine is damn nice. Please(!) look the the PHPTAL page and read the full documentation about the TAL syntax. In this tutorial the template were at a very basic level. You can do much more cool stuff and will have much fun with it ;)
Let's get dirty...

