PHP Templating

2 views
Skip to first unread message

Alan Smith

unread,
Oct 26, 2010, 1:30:41 PM10/26/10
to D. Berebi, zesty...@googlegroups.com
Great to hear you just jumped in, without even being a member!

I would prefer to not use a template system or generate the output. I'd rather do something like this: http://github.com/skrat19/codecabin/blob/master/app/views/page.php

Template systems mean learning a new "language" and we don't want this project to have any barriers to entry. Generating the HTML is extra code and to edit the HTML you'd have to know how the generator works. Plain HTML with the PHP embedded eliminates any of these hassles and is the most flexible/fast.

(I have added the mailing list to this discussion as someone else might wonder about this further down the road.)

Alan

On Thu, Oct 21, 2010 at 5:08 PM, D. Berebi <dbe...@gmail.com> wrote:
BTW, What do you think about template system or at least class to handle the generation of the output?
If we'll use such solutions it'll be much more easy to keep consistency

Alexis Métaireau

unread,
Oct 26, 2010, 2:23:51 PM10/26/10
to zesty...@googlegroups.com, D. Berebi
Alan, thanks for forwarding this here,

Le 10/26/2010 06:30 PM, Alan Smith a �crit :


> Template systems mean learning a new "language" and we don't want this
> project to have any barriers to entry. Generating the HTML is extra code and
> to edit the HTML you'd have to know how the generator works. Plain HTML with
> the PHP embedded eliminates any of these hassles and is the most
> flexible/fast.

Not sure it's most flexible, btw. I'm used to deal with this kind of
templates, and i can say, obviously, that's a BAD idea: this mean adding
a lot of logic in the templates, and tend to complexify the whole solution.

In PHP, you can have a look to the excellent TWIG, by fabien potencier,
and this article is a very good starting point:
http://fabien.potencier.org/article/35/templating-engines-in-php-follow-up.

I really think there is a value added by the fact of using a template
management system. It's really not that hard to learn, and simplify *a
lot* the comprehension of those ones.

Cheers,
Alex

Alan Smith

unread,
Oct 26, 2010, 3:40:06 PM10/26/10
to zesty...@googlegroups.com
Alex,

For purposes of comparison, say we have a php/html file with no business logic in it, what advantages do a template system offer in that case that plain php doesn't? I don't see any differences other than syntax. Enlighten me. I've already written a parser that allows the use of shorttags on any server, whether or not it's natively supported. When I think of php in html files I'm imagining it with short tags which are very clean and easy to read.

(All business logic will be outside of the "templates"/html files whether or not we use a template system. No mixing in the php, sorry gang.)

Alan

Gregory Mendez

unread,
Oct 26, 2010, 3:45:59 PM10/26/10
to zesty...@googlegroups.com
I don't know PHP (though I use it from time to time). Our current member system on the ambitiouslemon.com website was written by Alan and he used his own php framework. Not knowing php I've found it very easy (as far as these things tend to go) to work with and expand upon.

Alexis Métaireau

unread,
Oct 26, 2010, 5:09:52 PM10/26/10
to zesty...@googlegroups.com
Le 10/26/2010 08:40 PM, Alan Smith a �crit :

> Alex,
>
> For purposes of comparison, say we have a php/html file with no business
> logic in it, what advantages do a template system offer in that case that
> plain php doesn't? I don't see any differences other than syntax. Enlighten
> me. I've already written a parser that allows the use of shorttags on any
> server, whether or not it's natively supported. When I think of php in html
> files I'm imagining it with short tags which are very clean and easy to
> read.
Well, that's not true in any cases, and that's an old flamewar, that
exists for long time ago.

I must admit that I prefer to rely on template and logic-free systems,
because I have seen some really hard and crappy uses of
php-based-templates.

But, if you already have a working system, we could continue that way, I
was just advocating about my POV about that.

Cheers,
Alex

Alan Smith

unread,
Oct 26, 2010, 5:19:08 PM10/26/10
to zesty...@googlegroups.com
Alex,

Please describe to me the advantages you see in a template system. I was not in any way trying to start a flamewar. I want all php/html files to be as logic-free as possible; I agree completely.

Please enlighten me.

Alan

Mark Steve Samson

unread,
Oct 26, 2010, 8:14:13 PM10/26/10
to zesty...@googlegroups.com
I too was enjoying the benefits of template systems before. But that's also before I knew how to do proper separation of MVC. Limiting functions in views to echos, for-loops, model methods (I mostly use Propel so it's always getStuff()) and a few string methods makes it easy enough to comprehend. I also tend to forget things easily. Even if I memorize all the neat shorthand template methods in one project, I'd forget them in the next one. And you'd always end up trying to extend the template class if it doesn't have a feature you want. PHP functions are burned to my brain already (although I still look up argument order :P; damn PHP for not having named arguments).
--
Mark Steve Samson
Freelance Web Developer
http://marksteve.me

Alexis Métaireau

unread,
Oct 27, 2010, 5:02:07 AM10/27/10
to zesty...@googlegroups.com
Le 10/26/2010 10:19 PM, Alan Smith a �crit :

> Alex,
>
> Please describe to me the advantages you see in a template system. I was not
> in any way trying to start a flamewar. I want all php/html files to be as
> logic-free as possible; I agree completely.
Okay, cool,

One of the best thing about templating systems is the ability to extend
templates, via blocks. if any of you have done some django coding, or
using jinja2, you know what I'm talking about.

For the other ones, that are not familiar with this concept, we have
basically a "extends" directive, and a "block" one. So it's possible to
extend a already existing template, and just change parts of it.

Then, we do have some interesting things, like a value that's
auto-magically set to odd or even when looping on values, and other
things like that.

Another useful thing is a standardized way to handle objects and arrays
for instance (named hash or dicts in other languages). so, to access
$array['key1']['key2'], and $object->key1->key2, you use the same
syntax, that could be {{ var.key1.key2 }}, so no need to worry about
that here.

You also can auto-escape your variables before using it, this kind of stuff.

Then, that's not huge advantages, but I think they make code simple to
read and maintain.

Of course, we have access to all interesting stuff we could do in php
itself, but it's adding some facilities.

For more informations on templating systems I'm talking about, you can
have a look to Twig and Django:

http://www.twig-project.org/book/02-Twig-for-Template-Designers
http://docs.djangoproject.com/en/1.2/topics/templates/

Hope it's helpful !
Cheers,
Alexis
>
> Please enlighten me.

>
> Alan
>

Alan Smith

unread,
Oct 27, 2010, 10:35:45 AM10/27/10
to zesty...@googlegroups.com
Mark,

Thanks for your input. Hearing about your experiences with template systems was insightful and confirmed my reasons for disliking them.

Alex,

You have succeeded in enlightening me. While I comprehend the advantages of template systems I still do not wish to use them. The disadvantages (e.g. memorizing another syntax, when things are auto-escaped) is too much for this old dog. It's not that I can't do it but that the advantages aren't great enough to make me see the point. I don't understand why Twig would be so much better over plain php. The pros just don't outweigh the cons.

(Keep in mind that we want a low barrier to entry with this project. The easier it is to modify, the more people will help.)

Alex, if you wish to write up some code that extolls the virtues of Twig, send it to me. I will then write matching php code and it will be a fair comparison. I'm doing this so that any fear you have of plain php will be alleviated, not so I can say "my way is better than yours".

Know that if there's some Twig feature you love (or can't live without) we can always code it ourselves instead of using a full-featured system.

Alan

Alexis Métaireau

unread,
Oct 27, 2010, 10:46:43 AM10/27/10
to zesty...@googlegroups.com
Le 10/27/2010 03:35 PM, Alan Smith a �crit :

> Mark,
>
> Thanks for your input. Hearing about your experiences with template systems
> was insightful and confirmed my reasons for disliking them.
>
> Alex,
>
> You have succeeded in enlightening me. While I comprehend the advantages of
> template systems I still do not wish to use them. The disadvantages (e.g.
> memorizing another syntax, when things are auto-escaped) is too much for
> this old dog. It's not that I can't do it but that the advantages aren't
> great enough to make me see the point. I don't understand why Twig would be
> so much better over plain php. The pros just don't outweigh the cons.

Okay, no problems, it's a really old discussion and people can't agree
on that, no problems :)

> (Keep in mind that we want a low barrier to entry with this project. The
> easier it is to modify, the more people will help.)

I was thinking about the templating system about something to low the
barrier, but if you said it will raise it up instead, it seems better to
dont use one.


> Alex, if you wish to write up some code that extolls the virtues of Twig,
> send it to me. I will then write matching php code and it will be a fair
> comparison. I'm doing this so that any fear you have of plain php will be
> alleviated, not so I can say "my way is better than yours".

In fact, I'm not a Twig fanatic, and I've stopped writing PHP because
some things like that are not obvious and lead sometimes to have really
ugly quick and dirty code. Well, no problems, if we try to keep the
separation of the 3 tiers clear, it will work :)

No problems about the "who have the biggest one", I dont care, and it's
a really interesting discussion BTW.

> Know that if there's some Twig feature you love (or can't live without) we
> can always code it ourselves instead of using a full-featured system.

As I've said, there is an excellent article from Fabien Potencier (the
Symfony guy) about that: have a look, I agree a lot with him:
http://fabien.potencier.org/article/34/templating-engines-in-php

Alex
>
> Alan
>

Dinis Lage

unread,
Oct 27, 2010, 11:18:01 AM10/27/10
to zesty...@googlegroups.com
Hello,

I've spoken with Alexis about the template system and I from what I've read I still believe that is more useful only when you want to allow users or designers without any knowledge of the underlying language to modify it. The main disadvantage I see, although this depends on the framework, is the lack of ability to generate html code to iterate through groups of objects.
An example from the Yii framework is checkboxList helper:
http://www.yiiframework.com/doc/api/1.1/CActiveForm#checkBoxList-detail
This applies to most html stuff. And about escaping things, all done inside. One thing we might consider is migrating the project to a framework like Yii, CakePHP, or whatever.

Alan, what's the plan regarding this issue? Maybe start a new thread about the framework and introduce us to the current state of the framework/project? Or will we start from scratch?
Dinis
2010/10/27 Alexis Métaireau <ameta...@gmail.com>

Mark Steve Samson

unread,
Oct 27, 2010, 6:46:48 PM10/27/10
to zesty...@googlegroups.com
I have a hunch we will be building upon a framework by Alan or that we won't be using any third-party framework. I checked out Acorn and I liked how streams were used and how routing was implemented. I think we should first discuss the feature list so we could provide more input in existing libraries that would help. 

Alan Smith

unread,
Oct 27, 2010, 7:36:12 PM10/27/10
to zesty...@googlegroups.com
Mark,

Please take all discussion of frameworks to the "PHP Framework (was: PHP Templating)" thread. That will keep things orderly. Reply to what I'm about to ask, in that thread:

Discuss the feature list of Acorn or the hosting panel?

For an example of how Acorn can be used check out both Oak (http://github.com/skrat19/oak) and Code Cabin (http://github.com/skrat19/codecabin).

I don't want to push Acorn/Oak but if we're going to use a third-party framework it has to be flexible in structure and easily extended. If you can find a framework that strikes a balance between flexibility and doing the majority of the heavy lifting, as well as maintains simplicity, let me know! I built Acorn/Oak to fill this gap. If it has already been filled by a more mature project, that's great. If we can use a well supported library, that's the way to go. I'd rather use someone else's project that has a growing community and no chance of disappearing than my own code which I probably won't be able to provide endless support for.

I apologize for bashing frameworks and coming across as immovable in my position. I've looked at a lot of the php frameworks out there and none of them meet my standards. If there are some new kids on the playground that put the others to shame, tell me.

Alan

Alexis Métaireau

unread,
Oct 27, 2010, 9:56:59 PM10/27/10
to zesty...@googlegroups.com
Le 10/28/2010 12:36 AM, Alan Smith a �crit :

> For an example of how Acorn can be used check out both Oak (
> http://github.com/skrat19/oak) and Code Cabin (
> http://github.com/skrat19/codecabin).
Sry to be a bif off-topic, but that's amazing to see that I've made
exactly the same thing in python for my weblog. Have a look to
http://alexis.notmyidea.org/pelican/, Alan :)

Alex

Reply all
Reply to author
Forward
0 new messages