[PHPTAL] Include templates
lbedubourg
lbedubourg at motion-twin.com
Mon Jun 11 06:59:19 CEST 2007
Bob Black wrote:
> On Sun, Jun 10, 2007 at 08:51:06PM +0200, [BuKoX] wrote:
>> Joshua Paine pisze:
>>> You can do this:
>>>
>>> <metal:block define-macro="mymacro">
>>> some text with <b>something bold</b>
>>> </metal:block>
>
>> Ok. But my structure of my site is different. I have header.html:
>
>> and footer.html:
>>
>> I recieve exception in header.html that "Reached document end but
>> element stack not empty in:27" - last line in header.html. It's strange
>> becouse in Smarty it was very handy - I could include every template in
>> every place independent of template structure.
>> How can include templates in this way?
>
> You need to think about TAL a little differently than say html
> or php includes. (You can't "include" snippets of html, each
> template file needs to be a complete document, tags have to
> be matched, etc.)
>
> Basically what I do is make a site wide wrap.xml document that looks
> something like this.
> ------sample wrap-----
> <html>
> <head>
> [...]
> </head>
> <body>
> <div id='header'>
> <!--- header html -->
> </div>
>
> <div id='content' tal:content='structure: page_content' />
>
> <div id='header'>
> <!--- header html -->
> </div>
> </body>
> </html>
> ------end sample wrap-----
>
> (Note "structure:", otherwise html tags in the 'page specific' body
> be escaped.)
>
>
> Then for each individual page I setup and execute() the page specific
> template, and set it as $wrap->page_content. (Actually done in a site
> wide php file included as the last step of each page.)
>
> Hope that helps.
>
> Bob Black
Hi,
What Bob says is that you don't have to
<?php include('header.php'); ?>
foo bar baz
<?php include('footer.php'); ?>
For each template.
You can have ONE design.html which contains you base design in one macro :
<html metal:define-macro="main">
<head>
<title>Hello</title>
</head>
<body>
<div id="header">
<h1>Hello</h1>
</div>
<div id="menu">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
...
</ul>
</div>
<div id="content">
<metal:block define-slot="content"/>
</div>
<div id="footer">
Copyright (c) ...
</div>
</body>
</html>
And then you just call your design from each template :
<html metal:use-macro="design.html/main">
<body metal:fill-slot="content" tal:ignore-tag="">
<h1>My content</h1>
foo bar baz
</body>
</html>
You might have to write a little big more template code to achieve the
same result (because tal is quite verbose and metal is not so simple)
but each of your template will be valid XML, the final result will be
valid XML, you will have less templates files to manage and changing the
design will be a peace of cake.
Regards
Laurent
More information about the PHPTAL
mailing list