outline - a different sort of template engine for php

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

outline - a different sort of template engine for php

Post by mindplay »

Hello,

I would like to present my template engine, Outline, which is now nearly complete:

http://outline.mindplay.dk

Why yet another template engine? Yeah, you'd think there were enough at this point, but then you'd be wrong ;-)

I've been working with Smarty for some years now, and I've never been fully satisfied. For one, it's too big, and although it's still one of the fastest template engines for php, it's still at least five times slower than pure php templates (e.g. without an engine), which I've done for some of my own projects. For another, the syntax is far too bulky and complicated for my taste - and to dissimilar from php.

Outline implements, in under 1000 lines of code, all of the usual stuff like commands, blocks, modifiers, compiled templates, multi-level caching and so on. Unlike these engines, it makes no attempts to implement template security, and it does not validate the parts of the syntax that it borrows from php - which is a lot.

You can examine the syntax here:

http://code.google.com/p/php-outline/wiki/Syntax

Please examine it, if you're interested, and post your feedback, ideas, comments! thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: outline - a different sort of template engine for php

Post by Christopher »

I think the main problem with implementing a template library "for use by php developers and trusted template developers" is that you are competing against PHP itself. Using PHP itself is probably preferred over a complied/cached solution like this. Still, very nice work! :)

My only recommendation is to improve the configuration. I don't think all those constants are the best solution. You have that nice $config array. I would allow config values to be passed to the constructor in that form to overwrite the defaults and lose all the external defines.
(#10850)
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

Re: outline - a different sort of template engine for php

Post by mindplay »

I think the main problem with implementing a template library "for use by php developers and trusted template developers" is that you are competing against PHP itself. Using PHP itself is probably preferred over a complied/cached solution like this.
Well, you would think so. But how many projects that use Smarty were designed with user templates in mind? There are thousands of sites and projects that use Smarty, even though the templates are closed.

I know from personal experience, and from lots of other developers, that the real reason they choose Smarty is simply for it's more legible syntax and quick access to helpful things like modifiers, being able to easily reuse various design elements, and so on.

Very few sites and projects allow users to edit templates, and even for those that do, Smarty is not really safe as such.

Really, the only reason I designed this engine, is because I like my source code to be legible, and that includes my template sources. This way, I can get "near" full php performance, and without sacrificing the power and flexibility of plain php.

You're right though, it'll be hard to sell developers on the idea, because most of them don't seem to know why they're using a template engine. A lot of people still think it's because a template engine is necessary for proper separation of design logic and business logic. Shame on them. They will learn with time though ;-)
Still, very nice work! :)
thanks! :)

Code: Select all

My only recommendation is to improve the configuration. I don't think all those constants are the best solution. You have that nice $config array. I would allow config values to be passed to the constructor in that form to overwrite the defaults and lose all the external defines.
Were you looking at the latest version? Some of the last updates added a lot more configurability. The $config array is declared public, so you can change whatever you want in each individual instance - mostly the constants are still there to define a default configuration, for most options anyway. A few options should be global, and still are.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: outline - a different sort of template engine for php

Post by Christopher »

mindplay wrote:Were you looking at the latest version? Some of the last updates added a lot more configurability. The $config array is declared public, so you can change whatever you want in each individual instance - mostly the constants are still there to define a default configuration, for most options anyway. A few options should be global, and still are.
Not sure if I was looking at your latest version. It would be better to provide a method or via the constructor were you could pass a partial array of the config settings you want to change and it would overwrite the default. Plus be able to set them one-by-one. That would be better than just making a property public.
(#10850)
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

Re: outline - a different sort of template engine for php

Post by mindplay »

Not sure if I was looking at your latest version. It would be better to provide a method or via the constructor were you could pass a partial array of the config settings you want to change and it would overwrite the default. Plus be able to set them one-by-one. That would be better than just making a property public.
it should probably just be an optional parameter for the constructor - since there isn't any part of the configuration that you would benefit from changing, neither after or during rendering or compiling.
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

Re: outline - a different sort of template engine for php

Post by mindplay »

it is now configurable from the constructor, with error-checking for invalid configuration key names.

also, {include}'d templates now inherit the configuration from the main instance.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Re: outline - a different sort of template engine for php

Post by vigge89 »

Looks very interesting, I'll take a look and might come back with some input :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: outline - a different sort of template engine for php

Post by pickle »

Looks pretty sharp. I'd recommend doing 2 things:

1) Do a side-by-side feature comparison with other engines. Developers working with other engines (TemplateLite in my case) would hate to move to a new engine & find out some functionality they're expecting isn't there. It looks pretty full featured - even more useful in some instances, but a side-by-side comparison would sum it up nicely.

2) Do a speed test - write a huge template in a number of different engines such as Smarty, TemplateLite, raw PHP, & see how it compares. That could be a good selling point too.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

Re: outline - a different sort of template engine for php

Post by mindplay »

pickle wrote:Looks pretty sharp. I'd recommend doing 2 things:

1) Do a side-by-side feature comparison with other engines. Developers working with other engines (TemplateLite in my case) would hate to move to a new engine & find out some functionality they're expecting isn't there. It looks pretty full featured - even more useful in some instances, but a side-by-side comparison would sum it up nicely.

2) Do a speed test - write a huge template in a number of different engines such as Smarty, TemplateLite, raw PHP, & see how it compares. That could be a good selling point too.
1) wikipedia has a list of template engines for php, which includes Outline - there's a table comparing key features.

2) Ilia Kantor has create a template benchmark which includes a lot of the major engines. It does not include TemplateLite, but you are welcome to contribute. I contributed the implementation of Outline, which as you can see, is the fastest native PHP template engine.

I am going to, eventually, do a side-by-side comparison of template syntax features, comparing with Smarty syntax, as this is what most people already know - partially I'd like to make a point of demonstrating how much shorter Outline syntax is.

At this point though, my main focus is not on pushing Outline though, but on getting it finished, ready for production usage.

There are some interesting problems with caching that I still have to work out before I can tag that "1.0" on it...
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: outline - a different sort of template engine for php

Post by Bruno De Barros »

A lot of people still think it's because a template engine is necessary for proper separation of design logic and business logic. Shame on them.
I don't. For my projects, I think a template engine is unnecessary, as their owners will most probably never change their templates or, if they do, they'll contact me (because I provide free maintenance and support for all the projects I've done).

But the truth is, I would like to have some kind of engine that designers are used to, and as you said, Smarty is used by thousands.

I saw your template engine's syntax, and it looked kind of weird... But I am trying to learn it. A small, fast and powerful templating engine is exactly what I would like to have.

But what I don't really like is you caching the functions output. Does that mean that my modifiers are also cached, or is it only for functions? Because if they are, that would be pretty useless IMO.
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

Re: outline - a different sort of template engine for php

Post by mindplay »

Bruno De Barros wrote:I saw your template engine's syntax, and it looked kind of weird... But I am trying to learn it. A small, fast and powerful templating engine is exactly what I would like to have.
You're welcome to comment on the weird parts and suggest ways to "unweird" them :-)
Bruno De Barros wrote:But what I don't really like is you caching the functions output. Does that mean that my modifiers are also cached, or is it only for functions? Because if they are, that would be pretty useless IMO.
If you enable caching, functions and modifier output is cached, yes - same as in most template engines, isn't it?

Only thing that isn't cached, is the output from inserts.

If you didn't cache the output of inserts, there wouldn't be much you could cache in the first place, which means overhead from caching would end up outweighing the gain, making caching sort of pointless. Therefore, only insert bypass caching - so that only functions that specifically may not (or can't) be cached, won't be.

For most things you probably don't need caching anyway. I would recommend not blindly trying to cache everything, but rather use caching sparingly. Most of the time, page overhead comes from acquiring data, not from rendering the data in templates - a well-configured cache in MySQL, for example, will save you way more clockcycles than caching the output of templates, unless the templates are highly complex.

Bottom line is, most templates are essentially pretty simple, and you don't, for the most part, benefit from caching.
mindplay
Forum Commoner
Posts: 25
Joined: Sat Sep 16, 2006 1:32 pm

Re: outline - a different sort of template engine for php

Post by mindplay »

The first beta has been released for testing.

The bug submission has been enabled. Please use the message board on the site for feedback, requests, comments, etc.
Post Reply