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

Display template in side template

 
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
ThermoDust
Smarty Regular


Joined: 10 Sep 2003
Posts: 38

PostPosted: Tue Sep 16, 2003 10:29 pm    Post subject: Display template in side template Reply with quote

How can I include another template inside one or not depending on a if statement (The if statement is within the main tmeplate)?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Sep 17, 2003 4:24 am    Post subject: Reply with quote

Hi ThermoDust.

I'm not sure, but is sounds like you mean something like this:
Code:
{if $condition}
    {include file="subtemplate.tpl"}
{/if}

In this sample, if $condition is not empty or false, then the statements inside the if block will be executed, in this case, the inclusion of the template "subtemplate.tpl".

HTH
Back to top
View user's profile Send private message
dave f
Smarty Rookie


Joined: 19 Sep 2003
Posts: 8

PostPosted: Fri Sep 19, 2003 8:51 pm    Post subject: Reply with quote

Some templatesystems allow to assign either a variable to a placeholder or to replace it by another template.

Like replacing {$content} either by $tpl->assign( "content", $myVar ) or by $tpl->loadAnotherTemplate( "content", "anotherTemplate.tpl" )

Is there ay solution like that in smarty?
Back to top
View user's profile Send private message
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Fri Sep 19, 2003 9:25 pm    Post subject: Reply with quote

I think that is what you want.
[php:1:d4466be254]$smarty->assign('var', $smarty->fetch('another_template.tpl'));[/php:1:d4466be254]
Back to top
View user's profile Send private message
dave f
Smarty Rookie


Joined: 19 Sep 2003
Posts: 8

PostPosted: Sat Sep 20, 2003 9:48 am    Post subject: Reply with quote

Hm.. I'm not sure..

Does the template get parse when I fetch it or can I still assign vars to it?

I would like to assign some vars to both themplates after combining them..
Back to top
View user's profile Send private message
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Sat Sep 20, 2003 10:44 am    Post subject: Reply with quote

Quote:
I would like to assign some vars to both themplates after combining them..

You mean before, not after.

The only difference between display() and fetch() is that display() outputs the template directly and fetch() returns the output.

I suggest you take a look into the manual.
http://smarty.php.net/manual/en/api.display.php
http://smarty.php.net/manual/en/api.fetch.php
Back to top
View user's profile Send private message
dave f
Smarty Rookie


Joined: 19 Sep 2003
Posts: 8

PostPosted: Sat Sep 20, 2003 1:17 pm    Post subject: Reply with quote

Quote:
You mean before, not after.

Not I didn't mean that but that's the way it works..

The problem was just that from other templatesystems I'm used to load the template file first and then to assign vars directly to template files..

thx..
Back to top
View user's profile Send private message
Troublegum
Smarty Rookie


Joined: 07 Sep 2003
Posts: 33
Location: Germany

PostPosted: Sat Sep 20, 2003 3:59 pm    Post subject: Reply with quote

oh. Well I am not sure if that is possible with Smarty.
I do not know the templatesystem you are talking about, but when you mean you load the template first and then assign the content, the output is propably generated after you have assigned the content. What I mean is that you can load the template before, but the generation of the output usually happens after the assignment of content.
As far as I know, Smarty generates the output as soon as you call the fetch-method, so you must assign the content before.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Sep 20, 2003 4:16 pm    Post subject: Reply with quote

The most obvious way to combine templates is as I have shown above: using the {include} template function. If you don't want to hardcode dependencies, then you can also do it dynamically:

$smarty->assign('subtemplate', 'sub.tpl');
$smarty->display('main.tpl');

-- main.tpl --
This includes a subtemplate of unknown origin.
{include file=$subtemplate}

The trick is to assign the name of the template that will be included--not the contents of that template.
Back to top
View user's profile Send private message
al_nunes
Smarty n00b


Joined: 07 Oct 2003
Posts: 3

PostPosted: Tue Oct 07, 2003 2:44 pm    Post subject: Including more than one subtemplate dynamically Reply with quote

boots wrote:
$smarty->assign('subtemplate', 'sub.tpl');
$smarty->display('main.tpl');

-- main.tpl --
This includes a subtemplate of unknown origin.
{include file=$subtemplate}

How could I have more than one subtemplate in my file? Outside Smarty I would use variable variables. Is there a way to do something like:
for($i = 0; $i < sizeof($subtemplates); $i++)
{
$subtemplate = "subtemplate_" . $i;
{include file=$$subtemplate}
}
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Oct 07, 2003 2:59 pm    Post subject: Re: Including more than one subtemplate dynamically Reply with quote

al_nunes wrote:
How could I have more than one subtemplate in my file? Outside Smarty I would use variable variables.


variable variables: the worst plague since goto.
why not use an array and save yourself a lot of headaches?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
al_nunes
Smarty n00b


Joined: 07 Oct 2003
Posts: 3

PostPosted: Tue Oct 07, 2003 3:07 pm    Post subject: Reply with quote

boots wrote:
$smarty->assign('subtemplate', 'sub.tpl');
$smarty->display('main.tpl');

-- main.tpl --
This includes a subtemplate of unknown origin.
{include file=$subtemplate}

The trick is to assign the name of the template that will be included--not the contents of that template.

Other problem for a newbie like me. How can I assign contents to the template that is been included (sub.tpl)?
I'm having weird errors in my tries here.
Thanks
Back to top
View user's profile Send private message
al_nunes
Smarty n00b


Joined: 07 Oct 2003
Posts: 3

PostPosted: Tue Oct 07, 2003 3:13 pm    Post subject: Re: Including more than one subtemplate dynamically Reply with quote

messju wrote:
variable variables: the worst plague since goto.
why not use an array and save yourself a lot of headaches?


Could you send a little example? We must decide whether we will use Smarty or not quickly but I'm a lost here.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Oct 07, 2003 3:44 pm    Post subject: Reply with quote

an example for what? arrays?

index.php
[php:1:95cc0a5095]
<?php

/* assign a list of paragraphs to smarty, this list could have been pulled out of a database instead of being a hardcoded example */
$smarty->assign('paragraphs', array(
array('tpl_type'=>'foo1', 'text'=>"bla bla bla"),
array('tpl_type'=>'foo2', 'text'=>"bla bla bla"),
array('tpl_type'=>'foo1', 'text'=>"bla bla bla")
));
$smarty->display('index.tpl');

?>
[/php:1:95cc0a5095]

index.tpl
Code:

list of paragraphs:
{foreach from=$paragraphs item=paragraph}
{include file="paragraphs/`$paragraph.tpl_type`.tpl" text_content=$paragraph.text}
{/foreach}


the template renders each paragraph-element. depending on the $tpl_type-property there is a different sub-template used for each paragraph. (if tpl_type is "foo1" then template "paragraphs/foo1.tpl" is included).

a variable text_content is assigned to the inluded template, it contains the text-property of the paragraph.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dave f
Smarty Rookie


Joined: 19 Sep 2003
Posts: 8

PostPosted: Wed Oct 08, 2003 12:24 pm    Post subject: Reply with quote

That's the way it works for me without hard-coding the includes.

I have one outer template like
Code:

<html>..<table><tr><td>{$content}</td>...</html>


, one inner template like
Code:

<p>{$boxContent}</p>


and then assign different values to the inner template [php:1:e90743905b]<?php
$content = "";
$tpl->assign('boxContent', 'blaBla');
$body .= $tpl->fetch('innerTemplate.tpl');

$tpl->assign('boxContent', 'blubBlub');
$body .= $tpl->fetch('innerTemplate.tpl');

$tpl->assign('body', $body);
?>[/php:1:e90743905b]
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