|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Steve_K Smarty n00b
Joined: 01 Jan 2011 Posts: 1
|
Posted: Sat Jan 01, 2011 9:23 pm Post subject: Is it possible (or smart) to return an array from a plugin? |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Jan 02, 2011 4:24 am Post subject: |
|
|
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 |
|
|
|
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
|