0

I have a stclass object which i am initializing in PHP code. The object returns

object(stdClass)[238]
  public '0' => string 'Jun 20, 2012 03:02 PM' (length=21)
  public '1' => string 'Jun 20, 2012 03:26 PM' (length=21)
  public '2' => string 'Jun 21, 2012 01:12 PM' (length=21)

on doing var_dum($myObjectName)

I am passing this to smarty template where i need to access values of stdClass object at index [i] based on a javascript variable .

I have tried several of these:

{/literal} {$myObjectName}{literal} 
{/literal} {$myObjectName.0}{literal} 
{/literal} {$myObjectName.'0'}{literal} 
{/literal} {$myObjectName.'myLocalJSVariable'}{literal} 

but i am not bale to fetch value of Object at particular index

CodeMonkey
  • 2,265
  • 9
  • 48
  • 94

3 Answers3

0

Did you tried {$myObjectName->0}?

If you want to use it the way you use arrays, you must pass an array version of the object to Smarty:

$smarty->assign('myObjectName', get_object_vars($obj));

The get_object_vars() method returns a named array of all properties from an object.

lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
  • Which solution? The first, using the object itself, or the second, passing it as array? – lorenzo-s Jun 21 '12 at 20:28
  • the first one... i need to use the myObjectName->3 value in the template file. the number 3 is a javascript variable which i will assign to the code so as my code to access an object's member becomes like : {$myObjectName->javaScriptVariable} – CodeMonkey Jun 21 '12 at 20:32
  • What the output of `var_dump($yourObject)` from PHP? – lorenzo-s Jun 21 '12 at 20:33
  • object(stdClass)[238] public '0' => string 'Jun 20, 2012 03:02 PM' (length=21) public '1' => string 'Jun 20, 2012 03:26 PM' (length=21) public '2' => string 'Jun 21, 2012 01:12 PM' (length=21) public '3' => string 'Jun 21, 2012 01:25 PM' (length=21) – CodeMonkey Jun 21 '12 at 20:35
  • Ok, are you using Smarty2 or Smarty3? – lorenzo-s Jun 21 '12 at 20:35
  • I just wanted to try it by myself to help you, but I can't create an object like this. In PHP, variable and properties names cannot start with a number. – lorenzo-s Jun 21 '12 at 20:44
  • let me give you some more code.. that may help you what i am doing foreach ($variables as $var) { $myClassObject->$$var = $this->functionName($var1, $var2); } now here, $variables is an array with var_dump as array 0 => int 0 1 => int 1 2 => int 2 3 => int 3 – CodeMonkey Jun 21 '12 at 20:50
0

May I suggest you use json_encode() to dump your data to json, save that to something accessible to javascript, and then access your data from javascript?

{$myObjectName->javaScriptVariable} sounds just like you were trying to access some client data on the server, or some server data on the client - which is not possible unless you provide the data.

rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
0

Why don't you pass to smarty a reference to your object?

$smarty->assign('myObjectName', &$obj);

Then in smarty you can use the {$myObjectName->whatever} syntax...

napolux
  • 15,574
  • 9
  • 51
  • 70