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
@ before a function call will simply turn off error reporting for that
particular call. You will have to choose a different syntax. And, yes,
personally, I am against this suggestion. There are template engines that
already do similar things (Blitz, for instance) without requiring changes
in the language core.
2012/4/9 Tom Boutell tom@punkave.com
<?@addLinks($this->escape($foo)) ?
What if PHP supported a short tag for calling a method of $this?
Then one could write:
<?@escape($foo) ?>
A big -1 on this.
If you want to roll your own template syntax, just do so. It's not
that hard to run a quick str_replace('<?@', '<?php echo $this->',
$code) over the template.
Especially consider that @ is already used for error suppression, so
code that previously just wanted to suppress some errors will suddenly
try to call class methods (which obviously will lead to fatal errors).
Nikita
-1.
PHP doesn't need more magic.
On Mon, Apr 9, 2012 at 4:53 PM, Nikita Popov nikita.ppv@googlemail.comwrote:
What if PHP supported a short tag for calling a method of $this?
Then one could write:
<?@escape($foo) ?>
A big -1 on this.
If you want to roll your own template syntax, just do so. It's not
that hard to run a quick str_replace('<?@', '<?php echo $this->',
$code) over the template.Especially consider that @ is already used for error suppression, so
code that previously just wanted to suppress some errors will suddenly
try to call class methods (which obviously will lead to fatal errors).Nikita
There's a reason I didn't try to kick this out as a fully formed RFC (:
The choice of @ is a nonstarter, yes. I forgot that <? is a valid
start code for PHP already so it is already valid PHP to write <?@.
And I have no solution for the nesting problem. It's just an
interesting issue; if it had a good solution PHP might be a convenient
and extensible templating language, as well as being a fast one.
I don't think an explicit syntax for this would be "magic," however
terse it might be. Calling methods implicitly like Java does, that's
magic (:
-1.
PHP doesn't need more magic.
On Mon, Apr 9, 2012 at 4:53 PM, Nikita Popov nikita.ppv@googlemail.com
wrote:What if PHP supported a short tag for calling a method of $this?
Then one could write:
<?@escape($foo) ?>
A big -1 on this.
If you want to roll your own template syntax, just do so. It's not
that hard to run a quick str_replace('<?@', '<?php echo $this->',
$code) over the template.Especially consider that @ is already used for error suppression, so
code that previously just wanted to suppress some errors will suddenly
try to call class methods (which obviously will lead to fatal errors).Nikita
--
--
Tom Boutell
P'unk Avenue
215 755 1330
punkave.com
window.punkave.com
There's a reason I didn't try to kick this out as a fully formed RFC (:
The choice of @ is a nonstarter, yes. I forgot that <? is a valid
start code for PHP already so it is already valid PHP to write <?@.
Another tack to take is to use PHP's streams support. We did this in
ZF's Zend_View at one point to emulate short tags when they're disabled.
Basically, it allows you to pre-process the template file as it's being
included.
The downside is, of course, performance. However, you could very easily
add a caching layer to this as well to cache the modified code so that
subsequent calls do not have to do the pre-processing.
Basically, I think this is something that can be accomplished in
userland.
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
It definitely can be, yeah. I thought perhaps I'd hit on a way to do
it without as much redundant userland code to duplicate PHP's
templating behavior first and then make it better. But it's probably
not realistic to hardcode this one. No two people agree on templating
languages for long (:
On Mon, Apr 9, 2012 at 1:49 PM, Matthew Weier O'Phinney
weierophinney@php.net wrote:
There's a reason I didn't try to kick this out as a fully formed RFC (:
The choice of @ is a nonstarter, yes. I forgot that <? is a valid
start code for PHP already so it is already valid PHP to write <?@.Another tack to take is to use PHP's streams support. We did this in
ZF's Zend_View at one point to emulate short tags when they're disabled.
Basically, it allows you to pre-process the template file as it's being
included.The downside is, of course, performance. However, you could very easily
add a caching layer to this as well to cache the modified code so that
subsequent calls do not have to do the pre-processing.Basically, I think this is something that can be accomplished in
userland.--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc--
--
Tom Boutell
P'unk Avenue
215 755 1330
punkave.com
window.punkave.com
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.
You might be able to achieve this using the prep extension written by
Andrei Zmievski: https://github.com/andreiz/prep/
A relevant example can be found here:
https://github.com/andreiz/prep/tree/master/examples/array_literal where
he introduces an alternative syntax to array().
--
Christer Edvartsen
http://cogo.wordpress.com/
http://twitter.com/#!/cogocogo
-----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 PHPThere 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
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.williams1@ntlworld.com 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 PHPThere 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
Hi,
I'm not oppose to create a C module for OO template, but
new syntax is overkill.
Anyway, I would rather to have dedicated escape API for
- JavaScript strings
- JavaScript Identifiers
- CSS params
- CSS Identifiers
- what else?
It would be better to have dedicated escape functions so that
users will know they should escape/validate them.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net