Who wants to have a template engine discussion? :)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Who wants to have a template engine discussion? :)

Post by Luke »

I have never used a template engine of any kind in PHP. I have always preferred to use php's shorter syntax for my templates and just do my best to adhere to good practices within my view layer (not modifying variables, not pulling data from $_POST or anything like that). I have always said that php is already a great template language and that a template language just isn't necessary. Well... I believe I have changed my mind.

I believe the number one reason I have for changing my mind is Django.
The Django Template Docs Page wrote:If you have a background in programming, or if you’re used to languages like PHP which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.

The Django template system provides tags which function similarly to some programming constructs – an if tag for boolean tests, a for tag for looping, etc. – but these are not simply executed as the corresponding Python code, and the template system will not execute arbitrary Python expressions. Only the tags, filters and syntax listed below are supported by default (although you can add your own extensions to the template language as needed).
Using a template language (that isn't native language code) has a lot of benefits. First of all, it allows you to create your own, very strict set of functionality that is allowed in templates. This will automatically cause greater separation of presentation logic. It shouldn't be so easy to do the wrong thing. Second, it allows you to create the clearest, most concise syntax for the most complex and unclear things. For instance, template inheritance. I tried to implement a template inheritance system in plain php and it was sooo ugly and clunky looking. Look how sexy django's is.

base.html (layout file)

Code: Select all

<html>
    <head>
        <title>{% block title %}This is the default title{% endblock %}</title>
    </head>
    <body>
        {% block content %}
        {% endblock %}
    </body>
</html>
contact.html (extends base.html)

Code: Select all

{% extends "base.html" %}
{% block content %}
<h1>Contact Us</h1>
<p>To contact us, you'll need to find out where we are. Good luck!</p>
{% endblock %}
I have noticed most PHP developers I have discussed this with are completely against the idea of a template language on top of php. In fact most of them cringe if I bring it up. What's the deal with that? :? What is the problem with template languages? performance? I mean... you can precompile templates into vanilla php for performance...
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Who wants to have a template engine discussion? :)

Post by Eran »

For me the problem is that in many cases I need the native syntax of PHP that template systems cannot provide. I often pass objects into a view as opposed to static data, and expect to be able to call upon the object methods inside the view. If it were a traditional templating engine, that would be difficult.

In some cases, more expressive presentational logic is required which is outside the scope of templating. Why limit yourself to a set of stripped down functionality? I fully trust myself to make the right choices about what logic should be expressed in a view and what logic shouldn't. I don't want to restrict myself using an artificial handicap.
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Who wants to have a template engine discussion? :)

Post by sergio-pro »

Hi

Pytrin, are you sure you are making "right choices"?
I've never had a need to use method calls within template intensively. Maybe to make some formatting etc, but, i.e. Smarty allows you to make plugins for that. I'm afraid, you put too much logic to view, that is not good IMHO.

And if another two PHP coders start coding with you on the same project. They probably don't know about the right choices. They, maybe, even dont care of "separation of presentation logic" :)
So your project becomes a nightmare, in a couple of weeks.

There is a number of reasons here why to use templates: http://www.smarty.net/whyuse.php
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Who wants to have a template engine discussion? :)

Post by Christopher »

I like HTML templates and embedded tag template. Is there a port of the Django template system in PHP?
(#10850)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Who wants to have a template engine discussion? :)

Post by Eran »

I'm afraid, you put too much logic to view, that is not good IMHO.
No offense, but without seeing my code, you have no basis for making such statements. I do realize that for most web needs, using a templating engine can be an adequate solution. I've been dealing a lot lately with very complex information management systems with a highly evolved UI. I need the flexibility to implement custom logic, without resorting to hacks to the template engine.
And if another two PHP coders start coding with you on the same project. They probably don't know about the right choices. They, maybe, even dont care of "separation of presentation logic"
I have several developers working under me in my team. We have coding standards and we do peer review constantly. I rather they learn how to make good decisions than having their decisions made out for them and having to struggle against the infrastructure.
We've been working on this project for over half a year and it has still hasn't turned into a nightmare. In fact, we constantly refactor our view logic into more elegant solutions - which we can, since we are not limited by our templating engine.

We do use templates, just not a templating engine. Views are loaded in their own scope, and the engine is PHP.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Who wants to have a template engine discussion? :)

Post by VladSun »

I hate templates :)
My PHP projects don't use any HTML (except for the BODY tag and a few SCRIPT tags) - only JSON server-side views and ExtJS client side :)
Obviously, I don't create websites, so my opinion is not very important in this case, but I wanted to express my happiness with "free-of-HTML" programming.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Who wants to have a template engine discussion? :)

Post by Christopher »

VladSun wrote:My PHP projects don't use any HTML (except for the BODY tag and a few SCRIPT tags) - only JSON server-side views and ExtJS client side :)
I would be great if you would start a new thread on JSON server-side views and ExtJS client side. I'd be interested in learning more...
(#10850)
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Who wants to have a template engine discussion? :)

Post by sergio-pro »

I need the flexibility to implement custom logic, without resorting to hacks to the template engine.
Why don't you do it in a page controller?
I have several developers working under me in my team. We have coding standards and we do peer review constantly.
So you have a kind of internal specification for your team. But noone else knows about it. And, im sure, hundreds of other teams use their own specifications.
And using template engine makes it a king of common specification (which PHP lacks a lot IMHO).
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Who wants to have a template engine discussion? :)

Post by Eran »

Specifications is one thing, system architecture is another. We use a modified version of the Zend Framework, with very strict PEAR/ZF coding standards. Those are industry standards.
Why don't you do it in a page controller?
Presentational logic does not belong in a controller. And I don't use page controllers, but action controllers with a front-controller.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Who wants to have a template engine discussion? :)

Post by Luke »

arborint wrote:
VladSun wrote:My PHP projects don't use any HTML (except for the BODY tag and a few SCRIPT tags) - only JSON server-side views and ExtJS client side :)
I would be great if you would start a new thread on JSON server-side views and ExtJS client side. I'd be interested in learning more...
I'd be interested too.

As for a port of django's template system, I am not going to do a straight port of it, but the template engine I'm writing for my framework will be inspired heavily by django for sure. In fact, I plan on importing ALL of their native template tags (template tags would be sort of like view helpers) into my template engine. Also, I am considering adding a "php" template tag which would allow straight php, although it would be strongly advised against, and when debug is on, I just may have it bitch about the use of the php template tag... maybe.

My template engine will allow the use of method calls, but will not allow you to pass any args, so the only useful method calls would be getters (which are the only ones that make sense in the view). If you need special functionality in your view that the built-in template tags do not provide, the engine will be extensible in that you can create your own template tags.

All of the things I've mentioned my engine will do so far were inspired by django. I have also taken quite a bit of inspiration from Prado. That is a really neat framework and I really like some of their concepts.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Who wants to have a template engine discussion? :)

Post by Chris Corbyn »

If you want a solution that doesn't introduce yet another syntax, try playing with XML and XSLT. You provide your model as XML... the your template (XSL) creates a view in HTML. I also think you can extend a template with XSL (though I'm not an expert so I could be wrong, I've just seen it done in DITA).

Never feels nicer than PHP though.

I agree with pytrin too. IMHO, passing objects (just domain object mind you) into the view is an everyday practice for me. I don't want my page controller to know specifically *what* data it will use from my $auction domain object, I just know that my page displays an auction. What's so different about this:

Code: Select all

$this->view->set('auction', $auction);

Code: Select all

<h2 class="auctiontitle"><?php $this->escape($auction->getTitle()); ?></h2>
And this....

Code: Select all

$this->view->set('auctionTitle', $auction->getTitle());

Code: Select all

<h2 class="auctiontitle"><?php $this->escape($aucionTitle); ?></h2>
Your domain objects are just containers for information, and many believe it's a best-practice to allow your view to grab the data they need rather than being passed the information they need.

And no, don't move complex view logic into the page controller... that's simply not the correct place for it. View logic can get complex (when you've got loops full of conditions), but changing the foreach and the if..else syntax doens't remove the complexity, it just changes the syntax. It's still view logic at the end of the day.
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Who wants to have a template engine discussion? :)

Post by sergio-pro »

Presentational logic does not belong in a controller.
Presentation logic - is to display data. Something more complex than IF, FOREACH is not recommended in templates. (In mentioned Prado even IF was added not so long ago :) ). Because logic in template is not reusable, thus makes code duplicates.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Who wants to have a template engine discussion? :)

Post by Eran »

I guess this is the biggest difference between our approaches - my templates are highly reusable. I usually break them down into small reusable pieces that can be used completely independently in their own scope (partials, partial loops). Building most of my views out of small collection of highly reusable templates gives us very high flexibility, increases the consistency of the look-and-feel, and saves a ton of work. Generic reusable view logic is encapsulated in view helpers. There are a ton of ways to reuse view logic.

Loops can get pretty complex, and they are definitely a part of presentational logic.
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Who wants to have a template engine discussion? :)

Post by sergio-pro »

Yes, this is the difference.
Seems like you are on a half-way to a component framework (like prado).
Nested components - is something very similar to what you describe.
But still Prado don't use logic in template. Every component has template and php class having all the logic.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Who wants to have a template engine discussion? :)

Post by allspiritseve »

sergio-pro wrote:But still Prado don't use logic in template. Every component has template and php class having all the logic.
I highly doubt that. You can't completely separate display logic and HTML, except for the simplest of cases. It really comes down to presentation logic in your templates or HTML in your views... I will take the first option any day.
Post Reply