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 create a plain function for use in {if}?

 
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
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Tue Feb 07, 2017 10:33 am    Post subject: How to create a plain function for use in {if}? Reply with quote

I need to create a function for use in {if} structures in my templates and I can't find anything out of the available plug-in features that would make this easy. Twig appears to have this feature and it's used by Symfony:

Code:
{% if is_granted('ROLE_ADMIN') %}


in Smarty I would like this:

Code:
{if is_granted('ROLE_ADMIN')}


I would like to register this function in PHP (like with registerPlugin()) and use it in the templates. I know I could simply declare a global is_granted() function but that looks messy - I want the function only for use in the templates.

I can register the function with registerPlugin() and then use it like this:

Code:
{if {is_granted role='ROLE_ADMIN'}}


but this syntax is a bit convoluted. Can I register a function so that I can use the simpler syntax like in my earlier example?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 07, 2017 12:14 pm    Post subject: Reply with quote

You need a regular PHP function for that. No registering.
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