I will be redesigning code for a large website which uses Smarty and currently has 3 skins (3 separate sets of Smarty templates to display the same data). I will be keeping the templates as they are but write the application code from scratch since the current one is 7 year old and one big mess.
In many cases data are passed to Smarty in arrays and templates access those arrays like this:
<h2>{$product.name}</h2>
<p>{$product.short_description}</p>
<p><a href="{$product.link}">more info</a></p>
<p><a href="{$product.link}#accessories">Accessories ({$product.accessories_count})</a></p>
This is just a simplified example. Now I will be using a sort of active record in the application layer so in such cases it would be convenient to use objects directly like this:
<h2>{$product->name}</h2>
<p>{$product->short_description}</p>
<p><a href="{$product->getPageLink()}">more info</a></p>
<p><a href="{$product->getPageLink()}#accessories">Accessories ({$product->countAccessories()})</a></p>
So you can see that to objects having primarily data from db I will add many convenience methods like getPageLink() or countAccessories(). For me accessing those methods from within the templates is very convenient when I code but is this good design? Is accessing methods right for a template? Template designers should generally be not bothered with underlying business logic and I am wondering if methods should be used within templates. I use methods all the time in administration panels and I see no problem with this but this time I would use them in templates that will probably be edited by other people as well - designers who are not interested in my application code underneath.
What do you think? My question is what is your preference and how would you go about this? My current ideas are:
- Either recode the templates to use objects and methods and make it easier to maintain for me - the application coder
- Or leave the templates as they are and in the php code translate all necessary data from objects to arrays and pass only arrays to Smarty. This will not bother template designers at all but will add to the amount of code I will need to write.
but I’m still not sure which way is the better way, or perhaps there is a third way?