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

Variable in variable from config-file

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


Joined: 27 May 2004
Posts: 58

PostPosted: Thu May 27, 2004 4:35 pm    Post subject: Variable in variable from config-file Reply with quote

Hi,

i hope, that it isnt already posted somewhere here and that not too many found it out already Wink

I wrote in the config-file this

icon_0 = /images/icon0.gif
icon_1 = /images/icon1.gif

and so on

and in my mysql_db "topic_icon" should be a number (i.e. 0 or 1)

So it should display "/images/icon0.gif" when in db topic_icon=0


in the template:

Code:

{config_load file=d1.conf section=topic_icons"}     
{assign var="icon_number" value=$topics[idx].topic_icon}
{assign var="vari" value="icon_$icon_number"}

<img src="{$smarty.config.$vari}"></td>


is there a better way to do this?
if so, i didnt found it...
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu May 27, 2004 4:40 pm    Post subject: Reply with quote

If all your icons are named that way, do you really need a config file?

<img src="/images/icon{$topics[idx].topic_icon}.gif">
Back to top
View user's profile Send private message Visit poster's website
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Thu May 27, 2004 5:36 pm    Post subject: Reply with quote

sure, if sbd decides, the icons should be named like

aicon.gif
bicon.gif

and so on...it should all be variable
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


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

PostPosted: Thu May 27, 2004 5:58 pm    Post subject: Reply with quote

you can set $smarty->config_overwrite to false

then in your config-file
Code:

icon = /images/icon0.gif
icon = /images/icon1.gif


gives you an array. you can access that via {$smarty.config.icon.$i}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Thu May 27, 2004 8:08 pm    Post subject: Reply with quote

Shocked uhm...i didnt see that

ok...and is there a way to loop and display them?

(I actually do with a ini-function in php but when i set "icon" instead of "icon_0" i cant read the values)
Back to top
View user's profile Send private message Visit poster's website
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Thu May 27, 2004 8:25 pm    Post subject: Reply with quote

in php

$smarty -> config_overwrite = false;

in html

{assign var="icon_number" value=$topics[idx].topic_icon}
{$smarty.config.icon.$icon_number}

returns:

Fatal error: Smarty: [in templates/d1/index.tpl line 73]: syntax error: $smarty.config.icon.$icon_number is an invalid reference (Smarty_Compiler.class.php, line 1969) in /var/www/html/libs/Smarty_Compiler.class.php on line 2062

did i do sth wrong?
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


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

PostPosted: Thu May 27, 2004 8:34 pm    Post subject: Reply with quote

upgrade to Smarty-2.6.2 (the latest stable)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Thu May 27, 2004 8:38 pm    Post subject: Reply with quote

whew, fast answers here, respect Smile

ill do so, and post the results...

but...how to do a loop through the icons to display them?
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


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

PostPosted: Thu May 27, 2004 8:42 pm    Post subject: Reply with quote

since $smarty.config.icon is an array
{foreach from=$smarty.config.icon item=icon} ...use $icon... {/foreach}
should do
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Adar
Smarty Regular


Joined: 27 May 2004
Posts: 58

PostPosted: Thu May 27, 2004 8:47 pm    Post subject: Reply with quote

Very Happy thx

so u can delete this topic...its outdated
Back to top
View user's profile Send private message Visit poster's website
webadept
Smarty Regular


Joined: 09 Mar 2004
Posts: 41

PostPosted: Sat Jun 12, 2004 2:48 am    Post subject: Reply with quote

This thread just saved me so many hours in so many ways I can't thank you enoough, for the questions and the answers. Just gets better all the time don't it? Smile
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