|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
derek Smarty n00b
Joined: 17 May 2003 Posts: 2
|
Posted: Sat May 17, 2003 2:56 pm Post subject: Dynamically nesting templates |
|
|
Hello,
I am (finally) converting a PHP framework I have written from FastTemplate to Smarty. Now, my framework handles display by having "Component" objects that only know about themselves (with the applicable templates that only know about themselves). These Components are then nested, so that Component1 can be "attached" to the tag of some other Component2, and both Component1 and Component2 never need to know about it.
For example, main1.tpl doesn't care what goes into the {$body}. In some cases, I would put body1.tpl into {$body} and in other cases I would put body2.tpl into {$body}. And in other cases, I would want to put body1.tpl into SOME OTHER template (such as main2.tpl) in a similar fashion.
---- begin main1.tpl ----
<html>
<body>
<!-- I really don't know and don't care what goes in the body-->
{$body}
</body>
</html>
---- end main1.tpl ----
---- begin main2.tpl ----
<html>
<body>
<!-- I really don't know and don't care what goes in the body-->
{$body}
</body>
</html>
---- end main2.tpl ----
---- begin body1.tpl ----
<!-- contents of body1.tpl may get placed into main1.tpl OR main2.tpl -->
Sometimes I am the 'body' of this page but I never need to know about this other page and it never needs to know about me.
---- end body1.tpl ----
---- begin body2.tpl ----
<!-- contents of body2.tpl may get placed into main1.tpl OR main2.tpl -->
Sometimes I am the 'body' of this page but I never need to know about this other page and it never needs to know about me
---- end body2.tpl ----
So, in a nutshell, how can I assign a template to a tag, so that any given template doesn't need to know anything about any other template?
P.S. I am new at Smarty but have heard great things about it; I assume that this is possible with Smarty as FastTemplate does it. If it cannot, I will be absolutely shocked!
Thanks,
Derek |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat May 17, 2003 3:15 pm Post subject: |
|
|
depending on what you want to accomplish (sorry i don't know much about FastTemplate), i see two main approaches:
1. do it all at php-level:
Code: | $body = $smarty->fetch('body.tpl');
$smarty->assign('body', $body);
// now the template-variable {$body} contains the whole html-code
// and can be used as {$body} in the template |
2. assign the template-filename and include dynamically:
Code: | $smarty->assign('body', 'body1.tpl'); |
in the template to sth. like
Code: | <html>
{include file=$body}
</html> |
i almost always like 2. more, but it may depend on the rest of the application, which one suits better. and of course there may be other solutions these where the first 2 that came to my mind.
HTH
messju |
|
Back to top |
|
derek Smarty n00b
Joined: 17 May 2003 Posts: 2
|
Posted: Sun May 18, 2003 5:53 am Post subject: Thanks! |
|
|
Thanks!
The 'fetch' command does exactly what I needed, and my PHP framework is now officially converted to Smarty!
Derek |
|
Back to top |
|
Justin Smarty Regular
Joined: 07 May 2003 Posts: 38 Location: Vilnius, Lithuania
|
Posted: Sun May 18, 2003 6:27 am Post subject: |
|
|
and I'm still migrating I've used (and still using) phemplate, but Smarty seems to be more powerful. _________________ http://www.baubas.net |
|
Back to top |
|
JF1980 Smarty Rookie
Joined: 18 May 2003 Posts: 19
|
Posted: Mon May 19, 2003 8:40 am Post subject: |
|
|
Im using the second method you mentioned, good to know that Im not using a far out buggy logic! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|