There has been talk of making PHP a better templating language. After
all, it did start out there.

Folks have mentioned ideas like making it easier to output escaped
content, etc., but these are all hardcoded solutions. And one of the
biggest problems with PHP as a template language is that when best
practices change, you're stuck with the helper functions you already
have unless you start globally replacing things. You can't really
subclass or override a function.

So even frameworks that provide "helper functions" in the vein of
Symfony 1 (which borrowed the idea from Rails) get stuck when you want
to alter the behavior.

Last year I did a project in a one-off MVC framework of my own in
which I decided I didn't want to be stuck with that, so I made a rule:
anything I wanted to call from the template had to be a method of
$this, the view object in whose render() method the template file was
require()'d.

This turned out to be a good thing. By writing <?php
$this->escape($foo) ?>, I was able to benefit from whatever
implementation of 'escape' the subclass of view in question decided to
supply to me.

But of course it is very verbose and a templating language that is too
tedious to use won't get used.

What if PHP supported a short tag for calling a method of $this?

Then one could write:

<?@escape($foo) ?>

And 'escape' could be upgraded and modified as needed in an object
oriented way without the need to type <?php $this-> many times.

A problem with this proposal is that it does not address nesting. One
still has to write:

<?@addLinks($this->escape($foo)) ?>

And it is fairly common to combine such operations.

So maybe I should just define a sublimetext shortcut for:

<?php $this->

And be done with it. (: It detracts from readability relative to a
template language like Twig, but I can always choose to use Twig.

This would be notably easier if PHP, like Java and C++, called methods
of the current object implicitly without the need for $this->. But of
course that would be too great a change as there would be no way to
make existing code work correctly again if it reasonably expected
implode() to call the usual PHP function and not a method. Plus it's
probably a real pain to implement in general.

Thoughts?

-- 
Tom Boutell
P'unk Avenue
215 755 1330
punkave.com
window.punkave.com

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to