[Phplib-users] My modifications to template.inc
Brought to you by:
nhruby,
richardarcher
From: Peter B. <re...@f2...> - 2001-08-14 12:54:43
|
Kristian wrote: >There is no problem. Just release your changes to phplib as >LGPL, as the license requires you to do this. Your own >application is unaffected. OK, well here goes :-) I have developed a way of using PHP in the template files. Yes I know, the whole point of templates is to separate HTML and PHP. However, in my case I had developed a postcard application (sendcard - http://www.sendcard.f2s.com/ ) which people were using in existing sites. These sites were using included files for the design, and the people didn't want to keep a template with the design in as well, or rebuild the site using templates. So don't flame please, I coded to suit my users' needs, which at the end of the day is what it is all about. Credit for the code goes to Robin Vickery, who very kindly wrote the regular expression and function for me (I'm not that good with PCRE). I'm sure my code could be made neater by putting the functions into the class as well, but I'm not experienced enough with classes to do so. Just two notes before the code: 1. It will only work with PHP 4.0.5 or greater. 2. I subclassed template.inc and copied the whole function loadfile() and modified it. I then stuck the external functions below in the file. To add the code: At the bottom of function loadfile(), just before $this->set_var($varname, $str); add the following line: $str = preg_replace_callback('#<\?php(.+?)\?>#', 'evalTag', $str); And the external function: /* * This function replaces the PHP in the templates with the eval'd equivilent. * * @author Robin Vickery <ro...@ec...> * @param $match, the array of items matched using preg_replace_callback. * @return The eval()'d version of $match. */ function evalTag( $match ) { ob_start(); eval( $match[1] ); $replacement = ob_get_contents(); ob_end_clean(); return $replacement; } One other extension I have found useful for template.inc, which I hope will be included in a future version, was written by R.B. Scholtus <reg...@dd...> and is very useful for deleting a block from a template. function del_block($container, $handle) { $this->set_block($container, $handle); $this->set_var($handle, ""); } I hope these are useful to some people. The code is being used in sendcard 3, which will be released in less than a week, so download it if you are interested and try it out! Regards, Peter. ---oOo--- Do you sendcard? http://www.sendcard.f2s.com/ PHP postcard script supporting 9 databases! ---oOo--- Keywords for marc searchengine: PHP in template, delete block, execute template. |