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

How to get data from an array returned by an object

 
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
Nutron
Smarty n00b


Joined: 20 Nov 2003
Posts: 3
Location: San Francisco

PostPosted: Thu Nov 20, 2003 9:30 am    Post subject: How to get data from an array returned by an object Reply with quote

Let's say I've got this code:

Code:

class Thingy {
  function test () {
    $myArray['this']='something';
    $myArray['that']='something else';
  }
}

$thingy = new Thingy();
$smarty->register_object("thingy",$thingy);
....


So now in my template file I have:

Code:

{thingy->test assign="output"}


How do I get ['this'] and ['that'] out of $output? If I display {$output} in my template file I get "Array" so the object is there. If I loop through the array with a {section} tag, it'll loop twice. But no matter what I try I can't get it to print out the values.

I'll go ahead and list what I've tried:

{$output.this} = nothing prints
{$output['this']} = not valid syntax
{$output[this] = nothing prints
{$output[0]} = nothing prints

I know I'm getting my array back. How do I get the data?
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Nov 20, 2003 5:39 pm    Post subject: Reply with quote

you have to return $myArray from the method.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Nutron
Smarty n00b


Joined: 20 Nov 2003
Posts: 3
Location: San Francisco

PostPosted: Thu Nov 20, 2003 5:56 pm    Post subject: duh Reply with quote

Oh, right, but I do. I just didn't type it into my example.

Code:

class Thingy {
  function test () {
    $myArray['this']='something';
    $myArray['that']='something else';
    return $myArray;
  }
}


I do that. But when I do this:
{thingy->test assign="output"}

I can't figure out how to reference output['that'].
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Nov 20, 2003 6:03 pm    Post subject: Reply with quote

{$output.that} should do.
{thingy->test|@debug_print_var} should print the array.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Nutron
Smarty n00b


Joined: 20 Nov 2003
Posts: 3
Location: San Francisco

PostPosted: Thu Nov 20, 2003 6:25 pm    Post subject: Hmmmm Reply with quote

I figured it out. I was doing this (this is my real code):

Code:


<?php
// $album->getFolderInfo($dir_id) returns an array
// with name, dir_id, parent_dir_id, and description
$thisDir = $album->getFolderInfo($dir_id);
?>


Then in my template:

Code:

{album->getFolderInfo p1="$thisDir.dir_id" assign="output"}
{$output.name}


I know that thisDir already had these things, but I was just testing out the template. I tried $output.name, $output[name], etc. I also tried p1="$thisDir.dir_id", p1="'$thisDir.dir_id'", p1='$thisDir.dir_id' etc. but none of these would actually pass in the value (they would either pass in Array.dir_id or fail).

Then I did this:
Code:

{album->getFolderInfo p1=$thisDir.dir_id assign="output"}{$output.name}


And it works fine. so my problem was the quotes. Sheesh. I spent a lot of time figuring that out.

I'm only posting all this for others who may find themselves in a simlar situation.

Thanks for the help.


Last edited by Nutron on Thu Nov 20, 2003 10:06 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Thu Nov 20, 2003 7:00 pm    Post subject: Re: Hmmmm Reply with quote

Nutron wrote:
I figured it out. I was doing this (this is my real code):

Well, I'm sure glad you found it -- indeed, only use quotes where necessary, especially when passing arrays around. Otherwise, you get similiar results to those you'd get if you tried the same thing in PHP.

lol, it helps when the code snippets you post match your actual broken code example, huh Smile

Thanks for the follow-up!
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