Smarty PHP

0 comments | Posted: 9 August 06 in Books, by Nathan Smith

Smarty PHP Smarty PHP Template Programming and Applications is a great resource for both designers and developers who will be building sites or web applications with PHP. It was co-authored by Lucian Gheorghe, Hasin Hayder and João Prado Maia. Despite having three writers, it maintains a strong authorial voice throughout, showing practical examples of how to build PHP templates.

For an upcoming collaborative project, I needed to get familiar with Smarty. In case you have not heard of it, Smarty is a templating engine / framework for PHP. I would be surprised if you had not heard of PHP, because it’s hugely popular and is perhaps the most widely-used server-side scripting language in the world. Though it’s open source, even giant ‘net companies like Yahoo make use of it. In fact, the author of PHP Rasmus Lerdorf works for Yahoo.

If you are a designer, it is likely that you will work with PHP developers from time to time. As a PHP developer, you will probably want a good way to seperate templating logic from your core functionality. Either way, Smarty can help bridge that gap. If XHTML and PHP are people needing to communicate, Smarty could be seen as a bi-lingual interpreter. It also plays the role of peace maker, helping keep designers and developers out of each other’s way.

Most every PHP book out there has a chapter on Smarty. In his writings, Jason Gilmore refers to it as the “unofficially official” templating engine. This simply means that while there are a variety of options out there, Smarty seems to be the preferred one (as is evident by it residing on a sub-domain).

Despite being touted as the favored choice, to my knowledge this is the first book devoted entirely to explaining Smarty. At first, I wondered if there was really enough to be said to necessitate 250 pages, but after delving in further I realized just how expansive and flexible it can be. The book is divided into three main sections: the introduction, then a section each geared towards designers and programmers.

Designers will be happy to know that Smarty can be used to truncate text served from a database, not unlike abbreviated excerpts on a news page. Also, you can use a foreach loop, to take multiple data fitting various criteria, and arrange it in a logical fashion. For instance, you could take a listing of products provided to you by the PHP developer, and could display them in an unordered list, or sort them in an ordered list instead. You can also capitalize words or add whitespace, though tasks like these would be better left to CSS.

Programmers might like Smarty because it lets them pass variables for use in a template to a designer, who can then manipulate or rearrange them. The beauty of this is that all the core business logic stays on a separate layer, while the designer still has some flexibility for presentation. Smarty syntax is ultimately converted back into PHP for parsing into XHTML, which can be further managed by the programmer, and even cached to speed up page load time. If there is a need that cannot be met with core Smarty functionality, plugins can be used, and this book even shows you how to write your own.

If the description of either designer or PHP programmer fit you, then this book would probably serve as a great tool in collaborating with others who are more right/left brained than you are. It is also available as an eBook PDF version directly from the publisher. One of this book’s authors, Hasin Hayder has even provided a free Smarty cheat sheet that might prove useful – Get smart!

Discuss This Topic

Comments closed after 2 weeks.