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

refering to conf-file values using variables from tpl-files

 
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
icelander
Smarty n00b


Joined: 20 Dec 2003
Posts: 1
Location: Iceland

PostPosted: Sat Dec 20, 2003 12:36 pm    Post subject: refering to conf-file values using variables from tpl-files Reply with quote

Hi,
I just started moving a medium size php-project to Smarty.
I have a question on how to refer to values from configurations files using variables in template files ?

from php file:
Code:

$textId = GetTextId();
smarty->assign("text-id", $textId)


from conf file:
Code:

text-id1 = some text
text-id2 = some other text


from tpl file:
Code:

{config_load file="text.conf"}
{* now I need to display text from text.conf that has the id that $text-id refers to*}
{#$text-id#}


This will result in some error like:
Fatal error: Smarty: [in admin/Access/view.tpl line 24]: syntax error: unrecognized tag: #{$text-id(Smarty_Compiler.class.php, line 417) in d:\web\outdoors\smarty\Smarty_Compiler.class.php on line 2062

So the question is how to refer to values from configuration files using variables in template files ?

cheers,
Helgi Borg
www.fjallamennska.is
Back to top
View user's profile Send private message Visit poster's website
Duncan
Smarty Pro


Joined: 16 Dec 2003
Posts: 166

PostPosted: Sat Dec 20, 2003 8:08 pm    Post subject: Reply with quote

I don't think that it's possible either to define an array in the config or to pass dynamic data to a config variable within a template - at least I couldn't find anything about it in the docu.

A workaround would be to define an array in the php part and assign it to a smarty variable, e.g.:
Code:
$text_id = array( 1 => 'some text', 2 => 'some other text');
$smarty->assign("text_id", $text_id);

and then you can access it in the template via:
{$text_id.1}, {$text_id.2} where you can even provide the 1 & 2 via another variable - if needed.
Back to top
View user's profile Send private message
fbronx
Smarty Rookie


Joined: 28 May 2003
Posts: 11
Location: Stekene, Belgium

PostPosted: Thu Jan 08, 2004 10:39 am    Post subject: Reply with quote

The way I solved this problem is by creating a plugin:

Code:

function smarty_dynamic_config($params, &$smarty)
{
  extract($params);

  if ( isset($var) )
  {
     return $smarty->get_config_vars($var);
  }
  return '';
}


When this plugin is registered you can use it as follows in a template:

Code:

 {dynamic_config var=$a_config_var}
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Jan 08, 2004 2:57 pm    Post subject: Reply with quote

For one, "text-id" is not a valid variable name. But if you name it something like "text_id", you can do this:

{$smarty.config.$text_id}

( {$smarty.config.foo} is a synonym for {#foo#} )
Back to top
View user's profile Send private message Visit poster's website
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