Hi Gang,

I've been tasked with skinning a new 'static' site, and I'd like to use
PHPTAL to clean up my re-use of HTML, instead of just relying on SSIs or
repeating code over several HTML files.

I figured I would create a master template, and then use several smaller
templates to insert relevant html and variables into the master template.

So, I've created a master template using metal:define-macro:

> [template.html]
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en"
> metal:define-macro="master">
>     <head>
>         <title>Client</title>
>         <link rel="stylesheet" type="text/css" href="/css/screen.css"
> media="screen, projection, tv" />
>     </head>
>     <body>
> [...]
>             <div id="content" name="content"
> metal:define-slot="content"></div>
> [...]
>     </body>
> </html>
>

and then define a page view as such, using metal:use-macro:

> [index.html]
> <html metal:use-macro="template.html/master">
>     <body>
>         <div id="content" name="content" metal:fill-slot="content">
>             <h2>Welcome to Client</h2>
>         </div>
>     </body>
> </html>
>

Then I created a PHP file to invoke the PHPTAL template and use the
index.html file as its template:

> [index.php]
> <?php
> require_once 'lib/PHPTAL.php';
> $template = new PHPTAL('index.html');
> try {
>     echo $template->execute();
> }
> catch (Exception $e){
>     echo $e;
> }
> ?>
>

So far, this is working great - my <h2> appears inside my master template at
the right node.

*Problems with variables*
Now I want to start defining variables per page, that the master template
will use.  E.g. a <title> suffix, or attaching a class to the <body>
element.  So, in index.php I added the body_class and title_suffix
variables:

> [index.php]
> <?php
> require_once 'lib/PHPTAL.php';
> $template = new PHPTAL('index.html');
>
> $body_class = 'home';
> $title_suffix = ' - Home';
>
> try {
>     echo $template->execute();
> }
> catch (Exception $e){
>     echo $e;
> }
> ?>
>

And then made the suitable changes to my master template:

> [template.html]
> <title>Client<tal:block tal:content="title_suffix"/></title>
>

Now when I hit /index.php I get the following error:

> exception 'PHPTAL_VariableNotFoundException' with message 'Unable to find
> variable 'title_suffix' in current scope'



So, 3 questions:

   1. How and where should I define global variables that'll be used by the
   master template?
   2. How do I get variables defined by the calling template to be used by
   the master template?
   3. Or is there a better way to approach all this?


Thanks for your time and help!
Shaun

P.S. I'm a PHP newb.
_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to