|
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 |
rudder Smarty Rookie
Joined: 20 Oct 2009 Posts: 14
|
Posted: Wed Sep 07, 2011 2:45 pm Post subject: Best practices: PHP5 namespaces |
|
|
We've moved to Smarty 3 recently, and have been using PHP5.3 for a while. What we've noticed is that Smarty doesn't support namespaces in general, eg when calling static functions or using static vars/constants.
Doing some searches yielded really old threads where users (including administrators) note that any logic in the template, such as calling functions, was a bad practice, and to instead assign the results of functions to smarty variables, but over the years Smarty has supported more and more logic (eg being able to use objects and their methods directly in templates)
So my question is where do things stand now, and whether there are any plans to support namespaces in the templates, and what best practices are being considered by the Smarty administrators. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 07, 2011 3:37 pm Post subject: |
|
|
The answer is to register your class in the namespace in PHP:
PHP:
Code: | $smarty->registerClass("Foo", "\my\namespace\Foo"); |
Smarty:
Static method syntax was added relatively recently. Registered template functions and objects have always been around, not sure where that was said to be bad practice. |
|
Back to top |
|
rudder Smarty Rookie
Joined: 20 Oct 2009 Posts: 14
|
Posted: Mon Oct 03, 2011 10:14 am Post subject: |
|
|
Thanks for the reply mohrt. We'll use registerClass to accomplish our needs!
For bad practices, I was referring to posts in the past, such as your post here where you discourage using methods in IF logic:
http://www.smarty.net/forums/viewtopic.php?p=53138#53138
Specifically I know for instance we might have code that looks like this:
Code: | {if $user->can_delete()}{/if} |
And you commented that this is a break of business logic to template separation:
Code: | {if Permissions:check('delete',$user)}{/if} |
But the above is really no different than:
Code: | {if $user->permissions->check('delete')}{/if} |
or
Code: | {if $user->can('delete')}{/if} |
And IMO not a meaningful difference to template designers than:
Code: | {if $user_can_delete}{/if} |
In the past we used to assign all sorts of variables just like $user_can_delete or $user_is_logged_in but now we find this isn't as clean or powerful as directly accessing object methods and adds unnecessary clutter in the business logic (you also have to do more work to trace how "user_can_delete" is determined).
Is there a specific guideline to avoid using static methods in If statements vs object methods, or have those guidelines been mostly discarded now that Smarty is able to support so much of this syntax?
One way our usage has evolved for instance is the preference of object methods over assigned variables in many situations. One example might be looping through an array of products and retrieving the URl of the product image instead of hardcoding it in the template
Old method:
Code: |
{foreach $products as $product}
{if $product.show_image}
<img src="/path/to/images/{$product.image_filename}" />
{/if}
{/foreach}
|
New method:
Code: |
{foreach $products as $product}
{if $product->show_image()}
<img src="{$product->get_image_url()" />
{/if}
{/foreach}
|
The major benefit of the new method is that should /path/to/images/ change we have a single point that controls every path in the site (or in our case, the function returns different paths based on whether the person is on an SSL server or not, and whether it is a live vs dev environment). |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Oct 03, 2011 1:53 pm Post subject: |
|
|
Guidelines are just that, guidelines. If something works better for you, by all means use it. Smarty should be flexible enough to use as you see fit. We only suggest the simplest template syntax if possible. |
|
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
|