Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59506 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99016 invoked from network); 9 Apr 2012 15:10:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Apr 2012 15:10:20 -0000 Authentication-Results: pb1.pair.com header.from=tom@punkave.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tom@punkave.com; spf=pass; 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:44687] helo=mail-yx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4A/58-56433-BDBF28F4 for ; Mon, 09 Apr 2012 11:10:20 -0400 Received: by yenl5 with SMTP id l5so2128123yen.29 for ; Mon, 09 Apr 2012 08:10:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type :x-gm-message-state; bh=ivcIDp4WFdptWNq0zS23qpGtuO0M97dfBIPUl6f6oZ8=; b=hiQGWwegOACG59wN/F7MvG+tYgYPw1VStDaAkFoaJ2mK1QjknUPXqQp60+iBKHw4NH vnS7Pzkz0+UuCdI8UvgcZxRdeMMvlVjjmzQqdgpT0l+nZxB0FuZWVz2GZapgY7iJ/7sO rAOgNCHDMObbYclIbtq2HwkuIO9ipkfT52UPpi33HYkiZtG6gZnBrDxVJ+CDbm3IV6Y6 F14R9LWU4zw/PVNBa6KHKtUMRPfkE7a2Zh2614H5PLLMHsJMDmSMFGKmxkvWV5q6/FuF 6o/YYJbv7tPHksB1SosAJL+va4mM7AhYBlA+Rk2aD3PhGDiEgC0Z3e3GeAzmx0Yl2R5X EokQ== MIME-Version: 1.0 Received: by 10.101.3.33 with SMTP id f33mr1956672ani.80.1333984217242; Mon, 09 Apr 2012 08:10:17 -0700 (PDT) Received: by 10.100.35.16 with HTTP; Mon, 9 Apr 2012 08:10:17 -0700 (PDT) Date: Mon, 9 Apr 2012 11:10:17 -0400 Message-ID: To: PHP Internals Content-Type: text/plain; charset=ISO-8859-1 X-Gm-Message-State: ALoCoQnCdNfhMMUsQm2ypSvEi0zetlUp+RyTJOGJFwm6ab+7fX38vosfTkeuTcyOVM0FrcnYxS3P Subject: Object oriented page templates in PHP From: tom@punkave.com (Tom Boutell) 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 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? -- Tom Boutell P'unk Avenue 215 755 1330 punkave.com window.punkave.com