views:

43

answers:

2

Hi!

I'm hosting a multi area solution written in PHP, and each customer has its own template in some HTML files. Now I want these users to be able to use some chunks of dynamic content, but they can't be able to use PHP. I thought something like:

In the HTML file, if I put this:

<ul>[menu-list]</ul>

Will output something like:

<ul><li><a[...]>Home</a></li><li><a[...]>About</a></li>[...]</ul>

Is there any better way of doing it than keep parsing and caching files via file_get_contents() and preg_match_all()?

I want to create about 20 entries like [menu-list], and parsing every file for all of them seems quite expensive to me.

I'd appreciate any suggestion. =D

+1  A: 

Perhaps you should consider using a template compiler instead of a template interpreter. That is, instead of each time the page is loaded doing this whole replacement procedure you could simply perform the replacement after the template has been modified. During template editing the cost should be negligible. To implement this compilation you could choose to "compile" in some breadcrumbs so you can go backwards, or you can simply save the original template files for later editing.

Alternatively, you could consider using PHP variable naming conventions and running your templates through an eval, but this poses a number of other issues (like security threats) and doesn't come highly recommended.

Mark E
+1. This is a good idea (particularly the compiling part), but I would take a slightly different approach with regards to preserving the file for editing. I would regenerate the compiled file every time a change is made which would affect that file, but I would _also_ keep the entire original file intact.
Cam
Nice. I'll do something like this.
A: 

Why can't you use Smarty and friends? I would not want to write what you suggest myself.

middus
Smarty seems to be an awesome tool, but I I won't be able to train people, or even ask for knowledge in the `<smarty:>` tags.
How is [menu-list] better than smarty's tags?
middus