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

Can I use the parameter in {include} function?

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
rotee
Smarty n00b


Joined: 08 Aug 2003
Posts: 3
Location: Bangkok Thailand

PostPosted: Fri Aug 08, 2003 6:48 am    Post subject: Can I use the parameter in {include} function? Reply with quote

I wanna include the page depend on my parameter, such as :

{include file"$myPage"}

Can I do like this and how?

Thank you,
Rolling Eyes
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Fri Aug 08, 2003 8:10 am    Post subject: Reply with quote

you can do:
{include file=$myPage}

if you want to combine variables with constant text like:
{include file="main_`$chapter`.tpl"}

you should consult the manual at:
http://smarty.php.net/manual/en/language.syntax.quotes.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website
davewolf
Smarty Rookie


Joined: 25 Jan 2004
Posts: 5

PostPosted: Sun Jan 25, 2004 2:42 pm    Post subject: Reply with quote

Hi,

I have problem related to this issue, therefore I'm asking here.


Tested it as described in the manual. Works fine for {display ...}. {include file=$file_name} seems to work as well. But I always get a: Smarty error: unable to read resource: "" (for this example; tested with the demo page of the package).

Am I to ignore this, or to change anything?


Thanks in advance. Cool
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Sun Jan 25, 2004 3:07 pm    Post subject: Reply with quote

maybe $file_name is not set then.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
davewolf
Smarty Rookie


Joined: 25 Jan 2004
Posts: 5

PostPosted: Mon Jan 26, 2004 11:36 am    Post subject: Reply with quote

messju wrote:
maybe $file_name is not set then.

It is. It can be echoed without a problem, like...

{php}
$file_name = 'header.tpl';
echo $file_name;
{/php}
{include file=$my_name title=foo}


...from the index.tpl pf the demo page.

Tested it locally and on a remote server. I always get the correct echo and then this message:

Warning: Smarty error: unable to read resource: "" in /smarty/libs/Smarty.class.php on line 1144

No more problem if I switch back to...

{include file="header.tpl" title=foo}


BTW it's Smarty 2.6.0.


Any ideas?


TIA Cool
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Mon Jan 26, 2004 12:03 pm    Post subject: Reply with quote

make sure you have the value assigned: $smarty->assign('my_name', $file_name)

test in the template with "{$my_name|debug_print_var}" right before the {include}-tag.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
davewolf
Smarty Rookie


Joined: 25 Jan 2004
Posts: 5

PostPosted: Mon Jan 26, 2004 12:36 pm    Post subject: Reply with quote

messju wrote:
make sure you have the value assigned: $smarty->assign('my_name', $file_name)

test in the template with "{$my_name|debug_print_var}" right before the {include}-tag.

BTW, that was a typo on my side. It was actually:

{php}
$file_name = 'header.tpl';
echo $file_name;
{/php}
{include file=$file_name title=foo}


Tested different variable names, just in case some might be reserved and mixed things up when posting.


Anyway, I assigned the variable by Smarty standards, as you suggested, then it worked. Cool

Let's see if I got this:

All variables used by Smarty inside a template file must be assigned properly - As for this example just setting it with PHP satisfies PHP, but don't work for Smarty. Thus the correct echo, but a Smarty error. No difference for {include}, right?

Sorry for the newbie questions...


TIA

Dave
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Mon Jan 26, 2004 12:53 pm    Post subject: Reply with quote

right. you always have to assign variables, even from within {php} or from {include_php}:

[php:1:158ee89136]{php}
$file_name = 'header.tpl';
echo $file_name;
$this->assign('file_name', $file_name); /*
the template is displayed inside a method of smarty (mostly display())
so the $smarty object you called, is always "$this" inside php-tags */
{/php}
{include file=$file_name title=foo}[/php:1:158ee89136]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
davewolf
Smarty Rookie


Joined: 25 Jan 2004
Posts: 5

PostPosted: Sat Jan 31, 2004 11:12 am    Post subject: Reply with quote

Thank you, Sir!


It's amazing people like you keep supporting fools like me besides your real work. Exclamation Cool
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 -> Tips and Tricks 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