Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Dynamically nesting templates

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
derek
Smarty n00b


Joined: 17 May 2003
Posts: 2

PostPosted: Sat May 17, 2003 2:56 pm    Post subject: Dynamically nesting templates Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat May 17, 2003 3:15 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
derek
Smarty n00b


Joined: 17 May 2003
Posts: 2

PostPosted: Sun May 18, 2003 5:53 am    Post subject: Thanks! Reply with quote

Thanks!

The 'fetch' command does exactly what I needed, and my PHP framework is now officially converted to Smarty! Very Happy

Derek
Back to top
View user's profile Send private message
Justin
Smarty Regular


Joined: 07 May 2003
Posts: 38
Location: Vilnius, Lithuania

PostPosted: Sun May 18, 2003 6:27 am    Post subject: Reply with quote

and I'm still migrating Smile I've used (and still using) phemplate, but Smarty seems to be more powerful.
_________________
http://www.baubas.net
Back to top
View user's profile Send private message Visit poster's website
JF1980
Smarty Rookie


Joined: 18 May 2003
Posts: 19

PostPosted: Mon May 19, 2003 8:40 am    Post subject: Reply with quote

Im using the second method you mentioned, good to know that Im not using a far out buggy logic!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP