Re: Quick Question about Template Parsers
| From: | Brent Baisley | Date: | Wed, 22 Jan 2003 19:44:59 +0000 |
| Subject: | Re: Quick Question about Template Parsers | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-132672@lists.php.net to get a copy of this message | ||
Sure, you could do it any way you want. One way or another you need to get it into a variable, so how you get the template there doesn't really have any affect on your replace function.
I'm using the file based template so it's easy for users to modify. I "read" the template file into the output buffer, then store the buffer into a variable and clear the buffer:
ob_start();
include("templateFile.tpl");
$tpl_content = ob_get_contents();
ob_end_clean();
The template is now stored in the $tpl_content variable. You could just as easily pull the template from a database. For the template file I place "data tags" in it that I do a search and replace on. Some common "tags" I use:
{::PageTitle::}, {::MainData::},{::NavBar::}
You should try to use str_replace instead of ereg_replace since it's faster. Both support arrays for the search and replace part, so you can use one line of code to do 30, 40 or whatever replaces. I create an array of my data tags and another array of the matching data, then do:
str_replace($tagArray,$dataArray,$formTemplate);
I can then provide a user who knows HTML a list of data tags (the contract) that they can place in a template anyway they want. The whole idea is to separate your data from your code from you presentation.
Hope that helps.
On Wednesday, January 22, 2003, at 01:06 PM, Jim Lucas wrote:
In general, how do most template parsers work? do they read the file into a variable and do something to the effect of preg_replace() to element that is found? and if so, could you replace the step of reading the file into a variable with getting the template data out of a database? WARNING... more questions to come... Jim --Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577