PHP template engine

I'm designing a web app using the 3-tier pattern. I need a PHP template engine with good performance, and I'm looking at Smarty.

Feature-wise it seems good. My only concern is speed; I'm trying to squeeze as much performance as possible out of a 360. Anybody know the performance drawbacks? Or, have other suggestions?

Edit: I'm looking at the Zend Framework, and it seems it might be a better option even though I have less experience with it. Anyone used it?

15 Replies

I used Zend a couple of years ago for a commercial product and found it to be pretty good. Not sure about the memory usage though.

Best would be to try it out and see how it behaves. Make sure you're using something like lighttpd and use FastCGI.

And obviously cache as much as you can :)

Is a template engine really necessary in order to implement the 3-tier pattern? PHP itself could be considered a template engine of sort, which is why some of the faster MVC frameworks out there (e.g. CodeIgniter) just use plain PHP with minimal object-oriented wrappings. If you don't like those frameworks, there are similarly fast and simple offerings not affiliated with any framework, such as Savant3 http://phpsavant.com/

Speed-wise, you can't beat a plain PHP-based system. Just tell your designer to use or instead of <% … %> or whatever else some other template engines use.

Interesting comparison of PHP framework performance (17 months old)

http://avnetlabs.com/php/php-framework- … benchmarks">http://avnetlabs.com/php/php-framework-comparison-benchmarks

PHP is a templating language/engine. It was designed to be one from beginning. That's why you always need to enclose PHP code in tags.

I strongly recommend using just PHP for templating, and with that, naturally, logic and presentation separation. MVC is not required although it's a neat paradigm to work with.

And if you want a PHP framework, I recommend CodeIgniter.

@Azathoth:

And if you want a PHP framework, I recommend CodeIgniter.

+1

Templating Engines in PHP by Fabien Potencier

http://fabien.potencier.org/article/34/ … nes-in-php">http://fabien.potencier.org/article/34/templating-engines-in-php

The no-framework PHP MVC framework by Rasmus Lerdorf

http://toys.lerdorf.com/archives/38-The … ework.html">http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

@krmdrms:

Templating Engines in PHP by Fabien Potencier

http://fabien.potencier.org/article/34/ … nes-in-php">http://fabien.potencier.org/article/34/templating-engines-in-php

This article is a bunch of personal opinions and preferences presented as absolute facts. Plus, it is biased to say Python is good, PHP sucks. May be, may be not, but that does not make PHP anything less than a template engine.

Shorthand not an option? Why not?

PHP is not a template engine because it is … ugly?

is not good but <% for b in a %> is?

PHP is a templating engine. Any other templating engine for PHP is just bad reinventing of the wheel. Besides, this topic has been hashed and rehashed countless times in the history of PHP.

I stopped reading when he said this:

> The PHP language is verbose. You need no less than 14 characters just to output a simple variable (and no, using the more compact

Why? If you're going to bitch about it taking 14 characters to output a variable, why ignore

EDIT: I briefly glanced over the rest of the article. He makes many generalizations about how horrible PHP is while at the same time using archaic or inefficient code to demonstrate how PHP is inefficient.

The problem with short open tags () is that they sometimes interfere with XML standards.

If you have short open tags enabled, and if one of your templates contain the above (which, by the way, is strongly recommended for XHTML pages), PHP thinks

This issue can be worked around, though, by writing:

'; ?>

It's a little tedious, of course, so I use a home-brewed send_xml() function which automatically adds the above (and an appropriate DTD as well) before printing the template.

But hey, it's your server, your PHP installation, and your app. Enable whatever setting that makes your life easy, as long as it doesn't compromise security 8)

It's a bit less complicated:

'?>

Besides, you don't need that to have valid XHTML (even for 1.0 Strict), plus there are issues with IE6 on pages with anything before doctype.

Also you don't really need to type the above line that much. For all the sites I did, I'd have to use once per site theme (for main template file), or per RSS master template.

@Mr Nod:

@Azathoth:

And if you want a PHP framework, I recommend CodeIgniter.

+1 +2

@Azathoth:

And if you want a PHP framework, I recommend CodeIgniter.

I recommend Symfony.

Any +1's? :D

@KipBond:

@Azathoth:

And if you want a PHP framework, I recommend CodeIgniter.

I recommend Symfony.

Any +1's? :D

here's one: +1

I'm using a minimal approach just based on PHP. I have a template class containing an array $variables and a function getcontent(). getcontents() extracts $variables and builds the page using a heredoc with references to the variables, and returns the completed page or page section as a string. Using __toString to return the result allows templates to be nested. Eg:

get_contents();}
public function get_contents(){return "";}
}
class mypage extends tpl {
function get_contents(){
extract($this->variables);
$contents = << $var1 $form 
HTML;
return $contents;
}
}
class myform extends tpl {
function get_contents(){
extract($this->variables);
$contents = <<...
HTML;
return $contents;
}
}
$html = new mypage();
$html->variables['var1']= 'test';
$form = new myform();
$html->variables['form'] = $form;
echo $html;
?>

The template classes usually also contain their own logic to set the $variables.

-alnr

-1 against Smarty :) PHP itself is a template engine (as mentioned in this thread).

Another -1 for Smarty. PHP itself works great.

Smarty is another layer and if you're not the only one doing is an this is going to be for users to develop templates/themes it's more support for you as they'll need to know a bit of PHP, HTML and Smarty….

If they just know HTML and learn a bit of PHP, they can get a lot done.

I know with the Gallery project used Smarty in version 2 and in version 3, they've done away with it and have moved strictly to PHP. For the better too, it's much easier to work on a template or theme in this manner.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct