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

calling static methods in if condition

 
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
damucz
Smarty n00b


Joined: 11 May 2006
Posts: 1

PostPosted: Thu May 11, 2006 10:16 am    Post subject: calling static methods in if condition Reply with quote

Is there any way how to call static method such as {if Permissions::check('delete', $user)} ?

I found thread about using calling static method directly (ala {Permissions::check()}), but the solution was a special smarty compiler function like {static class="Permissions" method="check" argument="..."}. It is useless for me cause I can't use it in condition.

I have no workaround beside $smarty->assign object instance to the template.

The same problem is with using encapsulated class constants like {Permissions::CAN_READ} instead of global constant {$smarty.const.CAN_READ}. Does anybody know a way?

Thanks,
Dan
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu May 11, 2006 5:26 pm    Post subject: Reply with quote

No, AFAIK it is not possible.

I usually only have a few static methods that I am interested in making available to my templates and I simply register them manually as plugin functions using the usual PHP array callback syntax eg: array('Permissions', 'check'). If you have a lot of methods to pack up like that, you can write some code to automate the process.
Back to top
View user's profile Send private message
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Thu May 11, 2006 10:43 pm    Post subject: Reply with quote

Best thing would be to create a modifier plugin for your custom security check... Make the modifier return a boolean value that you can then use in your if statement

Code:

{if $user|permissions:"delete"}
blah
{/if}

_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
frugalprogrammer
Smarty n00b


Joined: 19 Sep 2008
Posts: 2

PostPosted: Fri Sep 19, 2008 6:26 pm    Post subject: Reply with quote

It's code reuse, it's good practice, and I'm honestly surprised that Smarty would be so silly as to not include a simple pattern such as this in its codebase.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Sep 19, 2008 7:54 pm    Post subject: Reply with quote

You directly break the rules of template/business logic separation with this in the template. Keep your business logic out of the presentation layer. That is how Smarty is designed to work. So, one way to do it:

Code:
$smarty->assign('can_delete', Permission::check('delete',$user));


Then in the template, a very simple:

Code:
{if $can_delete} ... {/if}


The template (designer) doesn't care about the logic required to set this flag, it just needs to know true or false. You aren't bothered with the underpinnings of your business logic. This is a clear separation of business/presentation logic, and keeps the template syntax to a minimum.

If you really need to do this check in-line on the template, use a custom function/modifier wrapper, as TGKnIght suggested. This gives you the flexibility to send params to your check in the template, and follows the syntax guidelines.
Back to top
View user's profile Send private message Visit poster's website
linenoise88
Smarty n00b


Joined: 27 Oct 2009
Posts: 1

PostPosted: Tue Oct 27, 2009 3:53 pm    Post subject: work around for calling static methods in if condition Reply with quote

I know this is a response to an old post, but it came up when doing a google search. You can't do it directly, but you can cheat and make it work.
If you assign a variable:

$object = new object_class();
$smarty->assign('object_static', $object);

Then in the smarty template
{if $var == $object_static->some_static_method($param)}
...do something...
{/if}

Php can call :: static methods like they were -> instance methods when you have created a instance
object_class::some_static_method();
or
$object = object_class();
$object->some_static_method();
both should work.

Some posters suggested this might breach the business logic/presentation separation, but if you are using the information for presentation only as it appears and not making changes, I don't see how that would be the case.
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