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

Is it possible (or smart) to return an array from a plugin?

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
Steve_K
Smarty n00b


Joined: 01 Jan 2011
Posts: 1

PostPosted: Sat Jan 01, 2011 9:23 pm    Post subject: Is it possible (or smart) to return an array from a plugin? Reply with quote

Hi all,

I just started using Smarty today, and I really dig the flexibility and power. I apologize in advance if this question is trivial. The smarty documentation mentions that it's best practice to separate php functions from smarty templates; one should write plugins (usually) to accommodate one's needs.
At the moment, I've written a smarty plugin which parses a txt file into an array which it then returns. My logic at that point was to take the array, loop through it with a smarty foreach and then spit out what I needed. (In this case, list elements for each line in the array).

My question is: is this the best way to handle my situation? Or, would it be better to declare the function in my index.php file (or even an external function file) and then assign the resultant array to my layout template and then loop through in a more normal fashion?

Here's an example of how my plugin is currently called:

Code:
{read_txt_document filename='roster.txt'} // returns an array


How would I loop through this on a template level if this plugin implementation is, indeed, correct? (Here's what I've tried):

Code:
<ul>
{foreach from={from read_txt_document filename='roster.txt'} item=name}
  <li>{$name}</li>
{/foreach}
</ul>


Thanks very much for reading. Any advice is greatly appreciated.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Jan 02, 2011 4:24 am    Post subject: Reply with quote

The typical pattern is to have your func assign the value:


Code:
{read_txt_document filename='roster.txt' assign="foo"} // assign array to foo


Code:
<ul>
{foreach from=$foo item=name}
  <li>{$name}</li>
{/foreach}
</ul>


See other plugins for examples of using assign.
Back to top
View user's profile Send private message Visit poster's website
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 -> Plugins 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