tags:

views:

110

answers:

4

(Summary: My users need to be able to edit the structure of their dynamically generated web pages without being able to do any damage.)

Greetings, ladies and gentlemen. I am currently working on a service where customers from a specific demographic can create a specific type of web site and fill it with their own content. The system is written in PHP.

Many of the users of this system wish to edit how their particular web site looks, or, more commonly, have a designer do it for them. Editing the CSS is fine and dandy, but sometimes that's not enough. Sometimes they want to shuffle the entire page structure around by editing the raw HTML of the dynamically created web pages.

The templating system used by WordPress is, as far as I can see, perfect for my use. Except for one thing which is critically important. In addition to being able to edit how comments are displayed or where the menu goes, someone editing a template can have that template execute arbitrary PHP code.

As the same codebase runs all these different sites, with all content in the same databse, allowing my users to run arbitrary code is clearly out of the question.

So what I need, is a dumbed-down, idiot-proof templating system where my users can edit most of the page structure on their own, pulling in the dynamic sections wherever, without being able to even echo 1+1;.

Observe the following psuedocode:

<!DOCTYPE html>
<title><!-- $title --></title>
<!-- header() -->
<!-- menu() -->
<div>Some random custom crap added by the user.</div>
<!-- page_content() -->

That's the degree of power I'd like to grant my users. They don't need to do their own loops or calculations or anything. Just include my variables and functions and leave the rest to me.

I'm sure I'm not the only person on the planet that needs something like this. Do you know of any ready-made templating systems I could use?

Thanks in advance for your reply.

+4  A: 

Why don't you use Smarty?

http://www.smarty.net/

There is a security function with customisable settings designed for exactly what you need, editing via 3rd parties.

The other way you could do it is sanitize the input when they make changes to make sure they haven't included anything inappropriate

Cetra
Seconded, Smarty is awesome.
Chuck Vose
Smarty does indeed fulfil my most important requirement:"Security: Templates do not contain PHP code. Therefore, a template designer is not unleashed with the full power of PHP, but only the subset of functionality made available to them from the programmer (application code.)"Thanks a lot for your suggestion.But it also contains a million features I _don't_ need and would prefer not have to include.Perhaps I can pull out just the parts I care about.
Wilhelm
Well you can use the Unregister function to remove any functions you don't want:http://www.smarty.net/manual/en/api.unregister.function.phpIf you check the manual, there are a list of template functions that are included, you could just add a small array and a loop to remove them from the base php file using the unregister function.
Cetra
@Cetra why bother to remove? And who can be sure he removed everything?
Col. Shrapnel
Because it contains "a million features" Wilhelm doesn't need and would prefer not to include. There is a list of built in functions in the manual which, unless there's undocumented functions, can be removed if they aren't required. If there are undocumented functions you can just as easily open up the class file and have a poke around. I didn't actually say that he had to do that, just that the option is there to do it, rather than just "pulling out the parts".
Cetra
+3  A: 

I'd recommend Twig.

http://www.twig-project.org/

Particularity it's sandbox mode.

Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a templating language for applications where users may modify the template design.

As far as something that's as extremely simple as your example, why not just write a simple class to parse your templates? I don't think there's actually anything as super simple as you've requested being maintained and distributed as it's not very hard to write something up to do it.

Only other thing I can possibly think of is maybe mustache? Though I think it's PHP implementation is pretty early in development and I'm not sure how stable or usable it is at the moment.

http://github.com/bobthecow/mustache.php

anomareh
+1  A: 

http://dwoo.org/

Dwoo is a PHP5 Template Engine that was started in early 2008. The idea came from the fact that Smarty, a well known template engine, is getting older and older. It carries the weight of it's age, having old features that are inconsistent compared to newer ones, being written for PHP4 its Object Oriented aspect doesn't take advantage of PHP5's more advanced features in the area, etc. Hence Dwoo was born, hoping to provide a more up to date and stronger engine. So far it has proven to be faster than Smarty and it provides a compatibility layer to allow developers that have been using Smarty for years to switch their application over to Dwoo progressively.

Kevin
A: 

What's the difference between <!-- $title --> and <!-- header() -->?
Why not to make it the same style and then just do <!-- $header --> simple str_replace?

Col. Shrapnel