Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
 Post subject: PHP template engine
PostPosted: Tue Nov 17, 2009 10:46 pm 
Offline
Senior Member
User avatar

Joined: Sun Aug 10, 2008 11:26 am
Posts: 104
Location: ~$
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?


Top
   
 Post subject:
PostPosted: Wed Nov 18, 2009 3:35 am 
Offline
Junior Member

Joined: Sat Oct 24, 2009 2:16 pm
Posts: 21
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 :)


Top
   
 Post subject:
PostPosted: Wed Nov 18, 2009 2:48 pm 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
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 <?php echo ... ?> 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


Top
   
 Post subject:
PostPosted: Mon Dec 07, 2009 9:08 am 
Offline
Senior Member

Joined: Mon Dec 07, 2009 6:46 am
Posts: 331
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 <?php ... ?> 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.


Top
   
 Post subject:
PostPosted: Mon Dec 07, 2009 12:43 pm 
Offline
Senior Member
User avatar

Joined: Tue Mar 17, 2009 5:11 am
Posts: 129
Location: UK
Azathoth wrote:
And if you want a PHP framework, I recommend CodeIgniter.


+1


Top
   
 Post subject:
PostPosted: Tue Dec 08, 2009 2:41 am 
Offline
Senior Newbie

Joined: Wed May 27, 2009 9:43 pm
Posts: 18
Templating Engines in PHP by Fabien Potencier
http://fabien.potencier.org/article/34/ ... nes-in-php

The no-framework PHP MVC framework by Rasmus Lerdorf
http://toys.lerdorf.com/archives/38-The ... ework.html


Top
   
 Post subject:
PostPosted: Tue Dec 08, 2009 5:24 am 
Offline
Senior Member

Joined: Mon Dec 07, 2009 6:46 am
Posts: 331
krmdrms wrote:
Templating Engines in PHP by Fabien Potencier
http://fabien.potencier.org/article/34/ ... nes-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?
<? foreach ($a as $b) ?> 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.


Top
   
 Post subject:
PostPosted: Tue Dec 08, 2009 11:11 am 
Offline
Senior Member
User avatar

Joined: Tue May 26, 2009 3:29 pm
Posts: 1691
Location: Montreal, QC
I stopped reading when he said this:

Quote:
The PHP language is verbose. You need no less than 14 characters just to output a simple variable (and no, using the more compact <?= shortcut is not an option):


Why? If you're going to bitch about it taking 14 characters to output a variable, why ignore <?= which lets you do it in less? Such arbitrary limitations just make for an unfair comparison.

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.


Top
   
 Post subject:
PostPosted: Tue Dec 08, 2009 12:35 pm 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
The problem with short open tags (<?= $var ?>) is that they sometimes interfere with XML standards.
Code:
<?xml version="1.0" encoding="UTF-8"?>

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 <?xml is a malformed opening tag and throws syntax errors.

This issue can be worked around, though, by writing:
Code:
<?php echo '<' . '?xml version="1.0" encoding="UTF-8"?' . '>'; ?>

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)


Top
   
 Post subject:
PostPosted: Tue Dec 08, 2009 1:00 pm 
Offline
Senior Member

Joined: Mon Dec 07, 2009 6:46 am
Posts: 331
It's a bit less complicated:

Code:
<?='<?xml version="1.0" encoding="UTF-8"?>'?>


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.


Top
   
 Post subject:
PostPosted: Thu Dec 10, 2009 1:49 pm 
Offline
Junior Member

Joined: Thu Feb 05, 2009 12:48 pm
Posts: 24
Mr Nod wrote:
Azathoth wrote:
And if you want a PHP framework, I recommend CodeIgniter.


+1
+2


Top
   
 Post subject:
PostPosted: Thu Dec 10, 2009 2:52 pm 
Offline
Senior Newbie

Joined: Wed May 06, 2009 2:49 pm
Posts: 19
Azathoth wrote:
And if you want a PHP framework, I recommend CodeIgniter.


I recommend Symfony.

Any +1's? :D


Top
   
 Post subject:
PostPosted: Fri Dec 11, 2009 7:39 am 
Offline
Senior Newbie
User avatar

Joined: Mon Nov 30, 2009 8:33 pm
Posts: 7
Website: http://kornrunner.net/
Location: Belgrade, Serbia
KipBond wrote:
Azathoth wrote:
And if you want a PHP framework, I recommend CodeIgniter.


I recommend Symfony.

Any +1's? :D


here's one: +1


Top
   
PostPosted: Fri Dec 11, 2009 9:54 pm 
Offline
Newbie

Joined: Sat Aug 16, 2003 4:43 am
Posts: 3
I'm using a minimal approach just based on PHP. I have a template class containing an array $variables and a function get_content(). get_contents() 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:
Code:
<?php
class tpl {
public $variables=array();
function __toString(){
   return $this->get_contents();}
public function get_contents(){return "";}
}
class mypage extends tpl {
function get_contents(){
extract($this->variables);
$contents = <<<HTML
<html><head> $var1 $form </body></html>
HTML;
return $contents;
}
}
class myform extends tpl {
function get_contents(){
extract($this->variables);
$contents = <<<HTML
<form>...</form>
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


Top
   
 Post subject:
PostPosted: Sat Dec 12, 2009 8:24 pm 
Offline
Senior Member

Joined: Thu Oct 08, 2009 5:07 pm
Posts: 99
-1 against Smarty :) PHP itself is a template engine (as mentioned in this thread).


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 4 guests


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

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group