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

Best practices: PHP5 namespaces

 
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
rudder
Smarty Rookie


Joined: 20 Oct 2009
Posts: 14

PostPosted: Wed Sep 07, 2011 2:45 pm    Post subject: Best practices: PHP5 namespaces Reply with quote

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Sep 07, 2011 3:37 pm    Post subject: Reply with quote

The answer is to register your class in the namespace in PHP:


PHP:
Code:
$smarty->registerClass("Foo", "\my\namespace\Foo");


Smarty:
Code:
{Foo::Bar()}


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
View user's profile Send private message Visit poster's website
rudder
Smarty Rookie


Joined: 20 Oct 2009
Posts: 14

PostPosted: Mon Oct 03, 2011 10:14 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Oct 03, 2011 1:53 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
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