Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Shorthand attributes in function plugins

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Sun Jul 07, 2013 12:31 am    Post subject: Shorthand attributes in function plugins Reply with quote

I have a number of functions that accept a single string parameter and return a value associated with that string (e.g., the option named 'siteName').

I would like to provide Smarty function plugins for each of these. I would also like to allow the designer to use a shorthand attribute to specify the desired option.

Example:
Code:
  {siteOption 'siteName'} instead of {siteOption opt='siteName'}


I have been able to get this working with compiler plugins, but not with function plugins. When I attempt to use a shorthand attribute with a function plugin, Smarty generates a "too many shorthand attributes" error.

I considered using compiler plugins but ran into an issue. Some options are true/false type values (return 1 or 0) and are used inside {if}...{/if} blocks.

With what I know about compiler plugins so far, I have not been able to get that to work.
My attempt was to return appropriate echo statements (e.g., echo 0).
What appears to happen is that Smarty uses ob_start()/ob_get_clean() to get the output of a compiler plugin when it's inside an if, assigning the ob_get_clean() to a temporary variable ($_tmp1).
Unfortunately, since ob_get_clean() returns a string, the variable becomes "0" instead of 0 and evaluates as true in the if.

So:
1) Is it possible to use shorthand attributes with function plugins and if so, how.
2) Is it possible to do what I want with compiler functions and if so, how Wink

Thanks in advance.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4653
Location: Hamburg / Germany

PostPosted: Sun Jul 07, 2013 10:19 am    Post subject: Reply with quote

1. Shorthands do not work with function plugins because by definition the passed params array must contain named attributes.

2. All returned values from plugins are expected to be strings because of the normal behaviour that plugin results are printed. So if you want to use plugin results in if conditions you must compare with corresponding strings or in your case the following work around
Code:
{if !empty({myplugin...})} ... {/if}
Back to top
View user's profile Send private message
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Sun Jul 07, 2013 6:01 pm    Post subject: Reply with quote

Thanks for the quick response!

using empty() did the trick.

Would it be worthwhile to make a feature request for shorthand attributes in function plugins?

Thanks
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4653
Location: Hamburg / Germany

PostPosted: Sun Jul 07, 2013 9:22 pm    Post subject: Reply with quote

I will look into it for the next major release.
Back to top
View user's profile Send private message
dpatterson
Smarty Rookie


Joined: 12 Apr 2013
Posts: 16

PostPosted: Mon Jul 08, 2013 4:11 pm    Post subject: Reply with quote

Great! Thanks.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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