开发者

Access php array in smarty

I have a object with a method like this: $foo->getId() which returns an integer, and i开发者_开发百科 have an array like:

$array(
     1=> array(
            "parent_id" => 14
     ),
     2=> array(
            "parent_id" => 15
     )
);

I need to access parent_id inside the subarray in smarty using the $foo->getId() as index key for $array, something like:

{$array[$foo->getId()].parent_id}

also tried just:

{$array[$foo->getId()]}

But both return error:

syntax error: unidentified token 

What am i not doing right?


You can try:

{$array.$foo->getId().parent_id}

If this does not work, I think you have to assign the ID to an other variable beforehand:

{assign var=foo_id value=`$foo->getId()`}{$array.$foo_id.parent_id}

In Smarty 3, this should work:

{$array.{$foo->getId()}.parent_id}


I have just tried to get the same error as you. Funny thing is, the code runs fine. Here we go for the specs: Smarty 3.0.7 with PHP 5.3.4.

My template code:

<html>
  <head>
    <title>Smarty</title>
  </head>
  <body>
    Hello, {$array[2]["parent_id"]}<br/>
    Hello, {$array[$foo->getId()]["parent_id"]}<br/>    
  </body>
</html>

The php file:

<?php

class Foo {

    public function getId() {
        return 2;   
    }   
}

// ... smarty config left out ... $smarty has been assigned successfully

$foo = new Foo();

$array = array(
   1 => array("parent_id" => 14),
   2 => array("parent_id" => 15)
);

$smarty->assign('array', $array);
$smarty->assign('foo', $foo);
$smarty->display('index.tpl');

?> 

The Output:

Hello, 15
Hello, 15


Try this:

$array[$foo->getId()]["parent_id"]


I will use a variable that way:

$a=array(
     1 => array(
            "parent_id" => 14
     ),
     2 => array(
            "parent_id" => 15
     )
);

Then you can access your array like this:

$a[1]["parent_id"]


First, don't forget to pass the tow variables to Smarty

$smarty->assign('array', $array);
$smarty->assign('foo', $foo);

And, in your Smarty Template, use :

{$array[$foo->getId()]["parent_id"]}


Never used smarty, but in PHP you can do:

<?php

class foo {
   public function getId() {
        return (int)2;
    }
}

$array = array(
     1 => array(
            "parent_id" => 14
     ),
     2 => array(
            "parent_id" => 15
     )
 );

$foo = new Foo;

echo $array[(int)$foo->getId()]['parent_id'];
//15

I've type casted as integer (int)$foo->getID() because the $array indexes are integers, enclosing them in brackets {} type castes them in strings.

(Maybe you should look at Foo::getID() and see if returns a string)

In smarty then, you could do something like this (theoretically, since I can't test it in smarty):

{$array[(int)$foo->getId()]['parent_id']}

//Also check if this works, but I suspect it shouldn't (the syntax it's not valid PHP)
{$array.(int)$foo->getId().parent_id}


Try This. From the php file assign object to smarty variable 'foo'

{assign var="val" value=$foo->getId()}
{$arr.$val.parent_id}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜