Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59594 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60752 invoked from network); 10 Apr 2012 01:51:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Apr 2012 01:51:14 -0000 Authentication-Results: pb1.pair.com header.from=jared.williams1@ntlworld.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jared.williams1@ntlworld.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ntlworld.com designates 81.103.221.48 as permitted sender) X-PHP-List-Original-Sender: jared.williams1@ntlworld.com X-Host-Fingerprint: 81.103.221.48 mtaout02-winn.ispmail.ntl.com Solaris 10 (beta) Received: from [81.103.221.48] ([81.103.221.48:17564] helo=mtaout02-winn.ispmail.ntl.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 73/1B-34074-012938F4 for ; Mon, 09 Apr 2012 21:51:13 -0400 Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20120410015109.IPHW23305.mtaout02-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com>; Tue, 10 Apr 2012 02:51:09 +0100 Received: from p2 ([82.3.19.10]) by aamtaout02-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20120410015109.IPFC3795.aamtaout02-winn.ispmail.ntl.com@p2>; Tue, 10 Apr 2012 02:51:09 +0100 To: "'Tom Boutell'" , "'PHP Internals'" Date: Tue, 10 Apr 2012 02:51:13 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 In-Reply-To: Thread-Index: Ac0WYvn4t7ewbEZ1Rc2jQuSFJLe4PQAV8v4g X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4325 Message-ID: <20120410015109.IPFC3795.aamtaout02-winn.ispmail.ntl.com@p2> X-Cloudmark-Analysis: v=1.1 cv=JvdXmxIgLJv2/GthKqHpGJEEHukvLcvELVXUanXFreg= c=1 sm=0 a=NzjuYBaO_hwA:10 a=uObrxnre4hsA:10 a=oHf_AeXmDeQA:10 a=kj9zAlcOel0A:10 a=ttyntS5QAAAA:8 a=_tEVJfvBQsVAqtKn1sYA:9 a=MgCFFkxAOMGAVv_ZtHUA:7 a=CjuIK1q_8ugA:10 a=VrdyGMU8UawA:10 a=U1yyyK3ObKtACP31:21 a=wF3jFvn8LgROr1p5:21 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 Subject: RE: [PHP-DEV] Object oriented page templates in PHP From: jared.williams1@ntlworld.com ("Jared Williams") References: > -----Original Message----- > From: Tom Boutell [mailto:tom@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 $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: > > > > And 'escape' could be upgraded and modified as needed in an > object oriented way without the need to type many times. > > A problem with this proposal is that it does not address > nesting. One still has to write: > > escape($foo)) ?> > > And it is fairly common to combine such operations. > > So maybe I should just define a sublimetext shortcut for: > > > > 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); Do I think it's a good idea? Probably not in this case. Jared