Jump to content

Archived

This topic is now archived and is closed to further replies.

redneonrt

PHP & templates

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]
<?php
function 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

Share this post


Link to post
Share on other sites
[!--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?

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.