There is probably a performance hit there when your view offers a lot of methods, but that is nevertheless a very clever solution (:
On Mon, Apr 9, 2012 at 9:51 PM, Jared Williams <jared.willia...@ntlworld.com> wrote: >> -----Original Message----- >> From: Tom Boutell [mailto:t...@punkave.com] >> Sent: 09 April 2012 16:10 >> To: PHP Internals >> Subject: [PHP-DEV] Object oriented page templates in PHP >> >> 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? >> > > $fn = [ > 'escape' => function($text) { return htmlspecialchars($text, > ENT_QUOTES|ENT_HTML5, 'UTF-8'); }, > ... > ]; > extract($fn); > > <?=$escape('blah')?> > > Do I think it's a good idea? Probably not in this case. > > Jared > > -- 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