View previous topic :: View next topic |
Author |
Message |
ThermoDust Smarty Regular
Joined: 10 Sep 2003 Posts: 38
|
Posted: Tue Sep 16, 2003 10:29 pm Post subject: Display template in side template |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Sep 17, 2003 4:24 am Post subject: |
|
|
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 |
|
dave f Smarty Rookie
Joined: 19 Sep 2003 Posts: 8
|
Posted: Fri Sep 19, 2003 8:51 pm Post subject: |
|
|
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 |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Fri Sep 19, 2003 9:25 pm Post subject: |
|
|
I think that is what you want.
[php:1:d4466be254]$smarty->assign('var', $smarty->fetch('another_template.tpl'));[/php:1:d4466be254] |
|
Back to top |
|
dave f Smarty Rookie
Joined: 19 Sep 2003 Posts: 8
|
Posted: Sat Sep 20, 2003 9:48 am Post subject: |
|
|
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 |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
|
Back to top |
|
dave f Smarty Rookie
Joined: 19 Sep 2003 Posts: 8
|
Posted: Sat Sep 20, 2003 1:17 pm Post subject: |
|
|
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 |
|
Troublegum Smarty Rookie
Joined: 07 Sep 2003 Posts: 33 Location: Germany
|
Posted: Sat Sep 20, 2003 3:59 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Sep 20, 2003 4:16 pm Post subject: |
|
|
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 |
|
al_nunes Smarty n00b
Joined: 07 Oct 2003 Posts: 3
|
Posted: Tue Oct 07, 2003 2:44 pm Post subject: Including more than one subtemplate dynamically |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Oct 07, 2003 2:59 pm Post subject: Re: Including more than one subtemplate dynamically |
|
|
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 |
|
al_nunes Smarty n00b
Joined: 07 Oct 2003 Posts: 3
|
Posted: Tue Oct 07, 2003 3:07 pm Post subject: |
|
|
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 |
|
al_nunes Smarty n00b
Joined: 07 Oct 2003 Posts: 3
|
Posted: Tue Oct 07, 2003 3:13 pm Post subject: Re: Including more than one subtemplate dynamically |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Oct 07, 2003 3:44 pm Post subject: |
|
|
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 |
|
dave f Smarty Rookie
Joined: 19 Sep 2003 Posts: 8
|
Posted: Wed Oct 08, 2003 12:24 pm Post subject: |
|
|
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 |
|
|