|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
icelander Smarty n00b
Joined: 20 Dec 2003 Posts: 1 Location: Iceland
|
Posted: Sat Dec 20, 2003 12:36 pm Post subject: refering to conf-file values using variables from tpl-files |
|
|
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 |
|
Duncan Smarty Pro
Joined: 16 Dec 2003 Posts: 166
|
Posted: Sat Dec 20, 2003 8:08 pm Post subject: |
|
|
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 |
|
fbronx Smarty Rookie
Joined: 28 May 2003 Posts: 11 Location: Stekene, Belgium
|
Posted: Thu Jan 08, 2004 10:39 am Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jan 08, 2004 2:57 pm Post subject: |
|
|
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 |
|
|
|
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
|