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

Can foreach fetch data 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 -> Smarty Development
View previous topic :: View next topic  
Author Message
maatin
Smarty n00b


Joined: 23 Sep 2004
Posts: 2

PostPosted: Fri Sep 24, 2004 12:05 am    Post subject: Can foreach fetch data from a plugin? Reply with quote

I'm quite new but already in love with smarty but there is a few things I cannot really understand. The thing bugging me at the moment is foreach's unability to fetch data stright from a plugin. I do this...:

Code:
/* 'cms4fetchAllUsers' is a plugin to query MySQL for all users
     in the usertable and output them as an array into the variable
     'target' designates */

{cms4fetchAllUsers target='allUsers'}

{foreach from=$allUsers item=user}

   /* yada yada yada with {$user} */

{/foreach}


...but I would like to do this:

Code:
{foreach from=cms4fetchAllUsers item=user}

   /* yada yada yada with {$user} */

{/foreach}


Am I turning iliterate or blind or is this not possible? I have scanned the forum and the doc a few times but can't find another solution.

/Martin
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
boots
Administrator


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

PostPosted: Fri Sep 24, 2004 2:06 am    Post subject: Reply with quote

Sounds like what you want is to package that as a modifer plugin, so you can do something along the lines of:
Code:

{foreach from="AllUsers"|cms4fetch item=user}
   /* yada yada yada with {$user} */
{/foreach}


Your modifier would return an array in this case.

It works Smile

HTH

PS. When writing function plugins that assign to template vars, you may want to consider using assign as the parameter name (instead of something like target) as that is the "standard" verbage in Smarty.
Back to top
View user's profile Send private message
maatin
Smarty n00b


Joined: 23 Sep 2004
Posts: 2

PostPosted: Fri Sep 24, 2004 12:19 pm    Post subject: Reply with quote

Thanks for the quick answer!

Well, that could do the trick but what I really would like is to have nothing but the functioncall instead of a variablename. I would like my smarty_function_cms4fetchAllUsers to return an array and not have to store the result in another array. Whenever I tried this approach the smarty compiler put tick's >'< around the function-name stopping it from working.

The "normal-programming-approach" would in php look like:

Code:
function fetch()
{
  /* ... */
  return $anArray;
}

foreach (fetch() as $item)
{
  /* ... */
}



No go? It won't work however I try?

Well, if not, the modifyer thing is quite allright


About the target keyword. It took me 20min to come up with and that was the best I had. I think I must have written assign at least 10 times during that period without even considering it. Jeez I'm thick sometimes. Better start using my dictionary again.

Cheers

/Martin
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
boots
Administrator


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

PostPosted: Fri Sep 24, 2004 4:18 pm    Post subject: Reply with quote

Hi.

Code:
Well, that could do the trick but what I really would like is to have nothing but the functioncall instead of a variablename. I would like my smarty_function_cms4fetchAllUsers to return an array and not have to store the result in another array. Whenever I tried this approach the smarty compiler put tick's >'< around the function-name stopping it from working.


Well, that defies Smarty's syntax, I'm afraid. What do you mean not store the result in another array? Are you talking about PHP's copy semantics? Returning from a plugin modifier or a plugin function is equivalent so I'm not sure what you are getting at.

You might want to write your own block function -- they can be used to write custom loop types and you can always have it decode a parameter (say 'from') as a function call instead of a variable lookup.

regards,
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 -> Smarty Development 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