5

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?

7
  • might have to use {php}{/php} for that $foo->getId() which ver smarty? Commented Mar 10, 2011 at 16:47
  • i'm using {$foo->getId()} in other parts without the {php} so that i know works, i'm not sure about the version, should be a recent one, let me check that. Commented Mar 10, 2011 at 16:54
  • @amosrivera But doesn't necessarily mean that you could use it as an array index. Commented Mar 12, 2011 at 22:18
  • certainly, me not knowing is exactly why i'm asking. Commented Mar 12, 2011 at 22:22
  • Or just switch to Twig. Despite of being faster and having more useful features like template inheritance, it will also allow what you are trying to do here: {{ array[foo.id].parent_id }} (or if you want an explicit method call: {{ array[foo.getId()].parent_id }}) Commented Mar 13, 2011 at 8:08

7 Answers 7

9
+50

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}
Sign up to request clarification or add additional context in comments.

1 Comment

Works also in if conditions, if someone would come across here and need it ;-)
3

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

Comments

1

Try this:

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

Comments

1

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"]

Comments

0

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"]}

Comments

0

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}

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.