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

{$array[#CONFIG_VALUE#]} syntax is not allowed. Another way?

 
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
ymo
Smarty Rookie


Joined: 03 May 2003
Posts: 14
Location: Tokyo

PostPosted: Sun May 04, 2003 3:32 am    Post subject: {$array[#CONFIG_VALUE#]} syntax is not allowed. Another way? Reply with quote

I want to use syntax '$array[#CONFIG_VALUE#]' in template.
But fatal error occurs.

Code:
Fatal error: Smarty: [in try.tpl line 15]: syntax error: unrecognized tag: $araay[#CONFIG_VALUE#] (Smarty_Compiler.class.php, line 382) in c:\Smarty-2.5.0\libs\Smarty_Compiler.class.php on line 1824


bellow is the code that I've tried.

Code:

-- PHP code --
<?php
$tpl = new Smarty();
$ar = array(0=>"ZERO", 1 => "ONE");
$tpl->assign('ar', $ar);
$tpl->display('try.tpl');
?>

-- try.conf --
INDEX_0 = 0
INDEX_1 = 1

-- try.tpl --
{config_load file="try.conf"}
<p>
index 0 = {#INDEX_0#}<br>
{$ar[0]}
</p>
<p>
index 1 = {#INDEX_1#}<br>
{$ar[1]}
</p>
{* -- Error -- *}
<p>{$ar[#INDEX_0#]}</p> <p>{$ar[#INDEX_1#]}</p>
{* -- Error -- *}


Please teach me, another syntax or solution.
Thanks.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 4:33 am    Post subject: Reply with quote

Right, the compiler does not allow config vars to be used as an index value. Even backticks won't help you there. Personally, I don't know of a syntax that can get this technique to work without using intermediate vars.

Can you do this instead?
$ar = array('INDEX_0'=>"ZERO", 'INDEX_1' => "ONE");

{$ar.INDEX_1} ?? It works.

Note, the following does not work either!:
Quote:
Set your index items up as an associative array in your PHP app and assign that array to Smarty.

$index_array = (array('INDEX_0'=>0, 'INDEX_1'=>1);
$tpl->assign("keys", $index_array);

{$ar[$keys.INDEX_0]}
{$ar[$keys.INDEX_1]}
Back to top
View user's profile Send private message
ymo
Smarty Rookie


Joined: 03 May 2003
Posts: 14
Location: Tokyo

PostPosted: Sun May 04, 2003 5:10 am    Post subject: Reply with quote

Thank you for your reply.
But it may not match my needs. It's my fault, too simplified.

I've imagin permision check procedures that PHP code and template are sharing one same config file.

Code:

-- perm.conf --
;this declaration is fixed another system.
PERM_ADMIN = 0
PERM_OPERATOR = 1
PERM_GUEST = 2

-- template --
{config_load file="perm.conf"}
{if $perm_array[#PERM_ADMIN#] == true}
 Show administration task menu
{elseif $perm_array[#PERM_OPERATOR#] == true}
 Show operator menu
{else}
 Show guest menu
{/if}

-- php code --
$perm_conf = parse_ini_file('perm.conf');
foreach($perm_conf AS $key => $value ) {
 define($key, $value);
}
// ** mistake **if (PERM_ADMIN) {
if ($perm_array[PERM_ADMIN]) {
 //Do admin task
} else {
 echo "You dont have permission.";
}


Any ideas?

Thanks.


Last edited by ymo on Sun May 04, 2003 5:23 am; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 5:22 am    Post subject: Reply with quote

Code:

-- template --
{config_load file="perm.conf"}
{if $perm_array[$PERM_ADMIN] == true}
 Show administration task menu
{elseif $perm_array[$PERM_OPERATOR] == true}
 Show operator menu
{else}
 Show guest menu
{/if}

-- php code --
$perm_conf = parse_ini_file('perm.conf');
foreach($perm_conf AS $key => $value ) {
 define($key, $value);
 $tpl->assign($k, $v);
}


Like that?
Back to top
View user's profile Send private message
ymo
Smarty Rookie


Joined: 03 May 2003
Posts: 14
Location: Tokyo

PostPosted: Sun May 04, 2003 5:39 am    Post subject: Reply with quote

Hmm, I understand.
And I understand I'm not smart ...

Thank you. Laughing
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 8:15 am    Post subject: Reply with quote

Actually, I think the syntax you were trying should be supported Wink
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 -> 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