"Smart" nesting of templates
| From: | 24hdev at chaoscontent dot com | Date: | Mon, 10 Jun 2002 11:35:55 +0000 |
| Subject: | "Smart" nesting of templates | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-4720@lists.php.net to get a copy of this message | ||
Hi there, I am standing in front of a serious design problem and would
appreciate some ideas.
--exposition--
Imagine having some different PHP classes representing different types
of data - an article class, a comment class and an image class.
I have a smarty template for each class: article.tpl, comment.tpl,
image.tpl. These templates need to be nested in their parent templates,
e/g header.tpl on top and footer.tpl on the bottom - this is achieved
using the smarty {include} function.
--problem--
Now my problem is that I want to be able to display data in two ways:
- display one class per page (i/e just ONE article)
- sequentially display many different classes at once on the same page
(i/e article on top, image somewhere, comments below).
If I do the latter, the header.tpl and footer.tpl are included every
time I use a new template, since the {include} function is present in
all of my sub-templates.
How would you avoid this? It should basically work that way:
- The first class (article) is displayed. Since the header was not
displayed yet, it is displayed first. Since there is more output later,
the footer is NOT displayed yet.
- The second class (image) is displayed. Since the header was already
displayed, it is NOT displayed again.
- The third class (comments) is displayed. Since this is the last, the
footer is displayed after everything else.
This sequence of three steps is just an example. It will change
depending on what content is requested.
Of course, I could go without the {include} function in the templates
and, in my PHP script, display the header.tpl FIRST, THEN display the
classes and FINALLY display the footer.tpl. However, I want to keep the
templates absolutely independent from my script - thus, the
"template-hierarchy" must be solely defined in the templates, not
hard-coded in my script.
This is giving me a headache ;)
I hope someone has a clearer view of how to cleanly achieve things like
this with smarty.
Yours
Sam