redneonrt Posted April 12, 2006 Share Posted April 12, 2006 I was looking at the some of the code that makes up PHPbb and what I see is the main page parses some sort of template.If I open up the template file I see things like this:[code]{SESSION}{TITLE}{HEADER}[/code]When the template is processed anything between {} gets converted to whichever variable is inside.Hope all that made sense now what I want to do is have something like that in a program I am working on. Anybody know how I might go about this? Quote Link to comment Share on other sites More sharing options...
Gaia Posted April 12, 2006 Share Posted April 12, 2006 You might want to take a look at some template engines such as [a href=\"http://smarty.php.net/\" target=\"_blank\"]Smarty[url] and [url=http://www.phpsavant.com/yawiki/]Savant[/a].These may or may not be what you are generally wanting, but they might put you in the right direction on what to look for more better. Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 12, 2006 Share Posted April 12, 2006 if you're wanting to simply know HOW it's done, take a look at the code snippet below for a very basic example:[code]<?phpfunction replace_all($String, $values) { $tags = array(); $rep = array(); foreach ($values as $key => $value) { $tags[] = '{' . $key . '}'; $rep[] = $value; } $String = str_replace($tags, $rep, $String); return $String;}$String = "{GREETING}<br />Thank you for coming today, {USER}!";$replace = array("GREETING" => "Hello!", "USER" => "obsidian");$String = replace_all($String, $replace);echo $String;?>[/code]hope this helps Quote Link to comment Share on other sites More sharing options...
redneonrt Posted April 12, 2006 Author Share Posted April 12, 2006 [!--quoteo(post=364089:date=Apr 12 2006, 11:00 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Apr 12 2006, 11:00 AM) [snapback]364089[/snapback][/div][div class=\'quotemain\'][!--quotec--]if you're wanting to simply know HOW it's done, take a look at the code snippet below for a very basic example:hope this helps[/quote]That looks more like what I am after.Now any ideas how I would write that so that that function processes a .tpl file? Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 12, 2006 Share Posted April 12, 2006 [!--quoteo(post=364096:date=Apr 12 2006, 12:17 PM:name=redneonrt)--][div class=\'quotetop\']QUOTE(redneonrt @ Apr 12 2006, 12:17 PM) [snapback]364096[/snapback][/div][div class=\'quotemain\'][!--quotec--]That looks more like what I am after.Now any ideas how I would write that so that that function processes a .tpl file?[/quote]well, you see where i've manually populated the $String variable? you just need to load the contents of the .tpl file into that variable, and it's the exact same principle:[code]$String = file_get_contents('myTemplate.tpl');$replace = array("GREETING" => "Hello!", "USER" => "obsidian");$String = replace_all($String, $replace);echo $String;[/code] Quote Link to comment Share on other sites More sharing options...
redneonrt Posted April 12, 2006 Author Share Posted April 12, 2006 Thats great,Thanks again everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.