Jump to content

PHP & templates


redneonrt

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?
Link to comment
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.
Link to comment
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
Link to comment
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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.