|
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 |
damucz Smarty n00b
Joined: 11 May 2006 Posts: 1
|
Posted: Thu May 11, 2006 10:16 am Post subject: calling static methods in if condition |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 11, 2006 5:26 pm Post subject: |
|
|
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 |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
frugalprogrammer Smarty n00b
Joined: 19 Sep 2008 Posts: 2
|
Posted: Fri Sep 19, 2008 6:26 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Sep 19, 2008 7:54 pm Post subject: |
|
|
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 |
|
linenoise88 Smarty n00b
Joined: 27 Oct 2009 Posts: 1
|
Posted: Tue Oct 27, 2009 3:53 pm Post subject: work around for calling static methods in if condition |
|
|
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 |
|
|
|
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
|