The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

All About Smarty - The PHP Template Engine

Although it's an excellent article something doesn't feel quite right when i read about Smarty ( or any other template-engine for PHP )

Because in essence PHP is already a template-language. The design of the language itself isn't that interesting ( flamewar alert ) it uses the best of all languages to pruduce the fastes results for its goal, which is to be the best webscripting language. What PHP makes interesting IMO are the modules, which allows you to do all sort of things.

As noted in the FAQ of Smarty, most template-engines are nothing more than a wrapper around a filereading and string substitution wrapper. I guess most of you already tried to write a simple template-parser script in PHP. THere are some smart feattures that makes Smarty preferable compared to other solutions, one of the most significant ones is that it compiles to PHP code. But the question remains:

Why would i want to have a PHP script that transforms my data into a presentation form using a language which goal is to do that.

If you look at profesional template solutions, you can already use a scripting language like Perl or Python in your templates, so if your point is that it's easier to learn for your (template) designer..it's not.

But enough about implementation, the real point why you would use a template-engine is that you wanna have a proper shifting of your data and your presentation. You want to transform your data into a view ( or presentation ) If you only need one view of your data, let's say a html presentation of your data like a website, a combination of PHP and a template-parser would be enough, but let's say i want different views on the same data. You'll soon hit some limitations.

A website can profit from a framework that uses different layers. In most situations, your view is gonna be generated from a fixed part of your data. Per page, a template is gonna be made and filled with your data-set. Whenever your presentation is gonna be altered and more data is needed, your ****ed. The code to deliver that data has to be altered as well, because your template is generated.

A solution would be to use an XML-XSL solution. First, your presentation is even more independent then your data. The presentation itself can use other data to make new combinations.

For clarity, it's often better to setup your structure not as a collection of HTML files. You'll often see a 1-on-1 mapping from data producers to HTML pages. With XML-XSL that isn't the case at all.

The beauty is that you concentrate on what data you have to offer instead what view you need. You don't have to worry on what presentation you gonna offer or the data the presentation might need. Your solution will be XML-based and with the help of XSLT you can transform data to (X)HTML. You could even combine differen data sources to achieve this.

Major advantage: you're working on an atomic level instead of concentrating about delivering your data.

I do have to agree it's an extra complicating factor. I'm certainly not the one to say webdesigners should write XSLT stylesheets. However, the framework laid out here using a clear seperation between the data you have to offer and the HTML presentation on the web certainly adds another level of professionalism and in the long run will be worthwile.

thanks so much for sharing your opinions and comments... for one i would like to get feedback on the apparent failure of the article

Why would i want to have a PHP script that transforms my data into a presentation form using a language which goal is to do that.

exactly the point one of our other members, voostind (vincent) raised in this thread... and yes i admit that there is a point to that but from my own experience and viewpoint, PHP has evolved much further than that and an even higher level of abstraction wouldn't hurt anyone, especially when there isn't much of a speed bump abstracting higher.... anyway, most of my points are in that thread so i won't bother to repeat them...

If you look at profesional template solutions, you can already use a scripting language like Perl or Python in your templates, so if your point is that it's easier to learn for your (template) designer..it's not.

what do you mean? you mean that professional template solutions require you to use Perl or Python? (you mean something else i think but i don't get it)

btw, my article's point is to bring across the features of Smarty and how those features make it 'outstanding' compared to the other PHP ones, like patTemplate, FastTemplate, phplib templates, etc.

A website can profit from a framework that uses different layers. In most situations, your view is gonna be generated from a fixed part of your data. Per page, a template is gonna be made and filled with your data-set. Whenever your presentation is gonna be altered and more data is needed, your ****ed. The code to deliver that data has to be altered as well, because your template is generated.

i disagree that you'd be ****ed (whatever that was )... if your presentation is gonna be altered, make the changes to your template (or create a new one); that part's no big deal admittedly... more data? that's exactly why you separate the business logic from the presentation... with templates, you just alter your code/script to reflect the changes to the business rules... i gotta admit i don't see your point...

A solution would be to use an XML-XSL solution. First, your presentation is even more independent then your data. The presentation itself can use other data to make new combinations.

// snip

I do have to agree it's an extra complicating factor. I'm certainly not the one to say webdesigners should write XSLT stylesheets. However, the framework laid out here using a clear seperation between the data you have to offer and the HTML presentation on the web certainly adds another level of professionalism and in the long run will be worthwile.

yes i appreciate the simplicity of using XML and XSL... that too in my opinion is the way to go... your point is one i agree with, but it is not something i'd throw to my template designers to do... perhaps when it catches on with more people, that day would come

First, I like the fact that I can completely seperate my presentation from my processing. This not only saves me the headache of tromping through PHP code to make HTML changes, but it also allows me to change the look of the entire website on the fly. This became very very useful when I developed my companies Website templates. My customers are able to login and choose a different template just by clicking a button. the whole site layout is altered by just changing what template to load. You could do this with php only, but why reinvent the wheel when someone has already accomplished that task.

Second, the caching features of Smarty are amazing. I am able to cache my website (vanndata.com) Whenever I make changes I simply delete the cache files or just wait until the current cache dies.

There is still alot I must learn about Smarty but I beleive it to be one of the strongest templating systems out there.

My customers are able to login and choose a different template just by clicking a button. the whole site layout is altered by just changing what template to load. You could do this with php only, but why reinvent the wheel when someone has already accomplished that task.

You're absolutely right! That wheel has already been invented, but for you're information, it wasn't by the Smarty-developers...

i won't use any of these template systems that are available. i guess just for the reason that they'd be too slow. the fact that Smarty has such a huge improvement with PHP/Zend Accelerator tells me that it must be really slow without an accelerator. every ounce of performance needs to be squeezed out of these things, but i don't think it's being done.

also, i don't like the idea of caching script output in most cases. just use an Accelerator. even if you're caching output, the script has to go through the expensive compilation without an Accelerator.

i've had to design my own templating system so that other users can change their templates. i've played around with different methods (eval(), str_replace(), preg_replace(), etc.) and i think i've figured out how to do it as fast as possible, but i still wish it would be much faster.

- Matt ** Ignore old signature for now... **
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3." "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

Well...this was my first post here and i didn't know someone already replied to the Smarty article. And i agree with voostind for 100%. Or 200%.

He hits the center of the 'problem' exactly and by the reactions of the posts i noticed lot alot of people fully understand his point. Like him, i studied Computer Science and have been working in the industry for awhile.

Originally posted by redemption
what do you mean? you mean that professional template solutions require you to use Perl or Python? (you mean something else i think but i don't get it)

nope. what i mean is already posted ( again by voostin so i''l quote him instead )

The template engine-specific language is nothing more than a minimal programming language with an ugly syntax. It has variables, conditionals, loops, and maybe even functions. As PHP is a full programming language, it has all of these constructs, and more. But that doesn't mean you require your web designers to know all there is to know about PHP. The set of requirements doesn't change at all. Just the basics will be enough.

Originally posted by redemption
i disagree that you'd be ****ed (whatever that was )... if your presentation is gonna be altered, make the changes to your template (or create a new one); that part's no big deal admittedly... more data? that's exactly why you separate the business logic from the presentation... with templates, you just alter your code/script to reflect the changes to the business rules... i gotta admit i don't see your point...

If you're gonna alter your presentation so, that you need more data, there's no doubt you'll have to change your code to deliver that data. Your template is gonna be 'filled'. With an XML-XSL solution your one step ahead because your presentation is even more independent then your data. It can use other sources to make new combinations.

Originally posted by dkode

First, I like the fact that I can completely seperate my presentation from my processing.

[..]

You could do this with php only, but why reinvent the wheel when someone has already accomplished that task.

[..]

Second, the caching features of Smarty are amazing.

[..]

There is still alot I must learn about Smarty but I beleive it to be one of the strongest templating systems out there.

You are not seperating the content from processing. In fact 90% of the php developers using some sort of template-engine are still busy with the presentation in the business-logic layer. Most of the scripts written in PHP consists of a few steps:

The essential point here is the 4th step. What if you want to depend your output on the dataset you processed. Or the input. Most developers will happy code that in the business logic, thus eliminating the main point of using a template-engine. Because that is what should be in your presentation layer.

And for the cache, what's the point of caching using a template-engine, and adding yet another step before actually displaying your content. PHP can do that and has all the functions for it ( filehandling, output caching ). Less code to execute...

Off the top of my head, here are some scenarios where a PHP template engine such as Smarty becomes useful:

When you want to have seperate PHP developers and HTML designers working on a project, and you don't want the designers messing around with the PHP code and potentially breaking it.

When you want to be able to quickly alter the HTML output of a script without having to delve in to the PHP code to find the HTMl you want to change.

When you want to provide alternative outputs for a script - miuch easier to just have different templates and let the application chose which one to execute.

When you want to keep your templates in a database for flexibility (makes it easier to write editing tools for them) - Smarty can load templates from a database rather than the file system.

At the end of the day, a template system is all about separating your application logic from your presentation logic. Obviously it isn't necessary in smaller scripts which just grab some content from a database and display it, but as soon as you start writing complex applications with PHP a template system becomes pretty much essential.

I don't want to spoil your fun, but all the points you mention here have already been examined (in that other post on Smarty) and questioned (by me)... You may want to look there, because it's little to no use to repeat that whole thread all over again here, now is it?

Take a look at this link, it covers most of the arguments, and explains Smarty pretty well.

I don't think that article explains Smarty at all. What it does is sum up a list of problems (all of which I completely agree with), and then it introduces Smarty as the solution to these problems.

As I said, I agree with the problems. but I don't think Smarty (or any other template engine) is the perfect, one and only solution. There are other solutions that solve the exact same problems, and they don't use templates at all.

You're absolutely right! That wheel has already been invented, but for you're information, it wasn't by the Smarty-developers...

It's called Cascading Style Sheets or CSS for short.

As I said, I agree with the problems. but I don't think Smarty (or any other template engine) is the perfect, one and only solution. There are other solutions that solve the exact same problems, and they don't use templates at all.

Vincent

Vincent already give good arguments about template-engines in this other thread and gives another good solution for these problems without templates: CSS!.

And yet none is taking into consideration to use CSS (in this thread), something I think is strange because it solves a lot (IMHO all of them) of the problems mentioned in this thread.

I now use XHTML 1.1 what forces you to use CSS. It gives me a lot of benefits:
- I can use different stylesheets for handheld, webtv, printing of a webpage, etc.
- I can change the complete layout, only with replacing the stylesheet.
- when the stylesheet can't be loaded\read (old browsers, missing stylesheet) the content can be still be read and still makes sense.
- the HTML-code is more readable and makes more sense
- no more tables for layout. I use DIV and SPAN to do this. This http://www.webmasterbase.com/article/379 explains how to begin with this.

I don't think that article explains Smarty at all. What it does is sum up a list of problems (all of which I completely agree with), and then it introduces Smarty as the solution to these problems.

As I said, I agree with the problems. but I don't think Smarty (or any other template engine) is the perfect, one and only solution. There are other solutions that solve the exact same problems, and they don't use templates at all.

Vincent

Well of course there is no perfect one solution, and Smarty doesn't claim to be. Smarty is *a* solution. It is up to you to identify your requirements and use a solution that works best for you. The point of the article is to explain how Smarty might be able to help you, not that it is the answer to every problem.

Originally posted by Skunk Off the top of my head, here are some scenarios where a PHP template engine such as Smarty becomes useful:

When you want to have seperate PHP developers and HTML designers working on a project, and you don't want the designers messing around with the PHP code and potentially breaking it.

When you want to be able to quickly alter the HTML output of a script without having to delve in to the PHP code to find the HTMl you want to change.

When you want to provide alternative outputs for a script - miuch easier to just have different templates and let the application chose which one to execute.

When you want to keep your templates in a database for flexibility (makes it easier to write editing tools for them) - Smarty can load templates from a database rather than the file system.

At the end of the day, a template system is all about separating your application logic from your presentation logic. Obviously it isn't necessary in smaller scripts which just grab some content from a database and display it, but as soon as you start writing complex applications with PHP a template system becomes pretty much essential.

But then you're putting HTML in your PHP files, which defeats the purpose of using templating systems in the first place - to separate presentation from content. Your approach is fine if you only need templates to put some sort of header and footer on each page, but if you want more control over the presentation you need to be able to change the layout of the page content itself.

Originally posted by tfountain But then you're putting HTML in your PHP files, which defeats the purpose of using templating systems in the first place - to separate presentation from content. Your approach is fine if you only need templates to put some sort of header and footer on each page, but if you want more control over the presentation you need to be able to change the layout of the page content itself.

in this case if u don`t wanna put some html code , u can use fopen, or include another html code, such tables and css or js code , and if use html u can give it css style, so when u change the template the html code which is in the php files will change as u change the css !

and 1 more thing ... whay the smarty use cache file ?
in the cashe file no need to use *_replace cmd , and this way is faster, that mean smarty is slow without cache files, with my way is more faster, without cache and no need to delete the old cashe files after each update !

As far as I can see, templates are about making sites managable and separating logic from design. Smarty, or other template engines do NOT do this. If/else statements are considered logic, so how can they be separated if you need them to display your design?

As far as I know, XML is really the best templating as you specify tags, then in the xml definition to tell the browser what to do with each tag, but then still, some logic has to be built into the design.

I've used Smarty templates in the past and once the template is made, you can reuse it by modifying the index.php file to suit your site and load your html in the body. It enables you to have one file for navigation, header and footer (if you prefer) without using frames (I personally don't care for them). That seems very simple to me.

Template should be about seperating *business* logic from *design* logic.
Things like looping to change colour of a row in a html page is perfectly acceptable use of logic in the presentation layer.
Using smarty to check whether or not the value returned from the Database is greater than or less than $x would be incorrect use since this is business logic and belongs in the application layer.
I am not a smarty user but this is how undertand it.