Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59602 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 79375 invoked from network); 10 Apr 2012 02:45:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Apr 2012 02:45:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=tom@punkave.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tom@punkave.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain punkave.com designates 209.85.213.170 as permitted sender) X-PHP-List-Original-Sender: tom@punkave.com X-Host-Fingerprint: 209.85.213.170 mail-yx0-f170.google.com Received: from [209.85.213.170] ([209.85.213.170:54213] helo=mail-yx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 77/0F-34074-3DE938F4 for ; Mon, 09 Apr 2012 22:45:40 -0400 Received: by yenl5 with SMTP id l5so2458286yen.29 for ; Mon, 09 Apr 2012 19:45:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding:x-gm-message-state; bh=9GTq5YXaQmMJuBMfcTBB+jZC6E1Qy1tRs+3NU+B1EXM=; b=gC2uXC351NljZEDpJYQhjUfdyWP2fMNfP0kY1PQTc28602DN2e8Jl15scmd3QoTj1q 81JK79apiiwd4QdA8/aVwhH2XeI9GAQHbAONyu8ljPyMaO0Zuy20M0cx8sGiSCP5Dpmz Pa11kGibZaITHGXE4mSykr6xD1vT/Vo2NtpduBl23JLUcdFYEFcnQhChAmMmk8P6NUGx l8LuNOrr0BtOTwFpjU+SWpDFXe7rrTLdgnnmUXUxCT4Ja2vNJ4Wle6VdKVDT98AQUXEx hmR3ujI5I1wFpTk6VJ7cyok81dwRVXUJIbcyr0FFMVjEA5ZnhWhnZ1YSsZqhSQnkGq+I 4Qdw== MIME-Version: 1.0 Received: by 10.101.166.32 with SMTP id t32mr2488433ano.41.1334025937424; Mon, 09 Apr 2012 19:45:37 -0700 (PDT) Received: by 10.100.35.16 with HTTP; Mon, 9 Apr 2012 19:45:37 -0700 (PDT) In-Reply-To: <20120410015109.IPFC3795.aamtaout02-winn.ispmail.ntl.com@p2> References: <20120410015109.IPFC3795.aamtaout02-winn.ispmail.ntl.com@p2> Date: Mon, 9 Apr 2012 22:45:37 -0400 Message-ID: To: PHP Internals Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQnSdtC8z/LUkQwzt45FAdpnX0fxT6tPbmQ6rGK3EaV5GqNw72nkRl1LYZArQJJNhiR2u9uf Subject: Re: [PHP-DEV] Object oriented page templates in PHP From: tom@punkave.com (Tom Boutell) 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 wrote: >> -----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 =3D [ > 'escape' =3D> function($text) { return htmlspecialchars($text, > ENT_QUOTES|ENT_HTML5, 'UTF-8'); }, > ... > ]; > extract($fn); > > > > Do I think it's a good idea? =A0Probably not in this case. > > Jared > > --=20 Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com