Jump to content


Photo

PHP & templates


  • Please log in to reply
5 replies to this topic

#1 redneonrt

redneonrt
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 12 April 2006 - 03:19 PM

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:
{SESSION}

{TITLE}
{HEADER}

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?

#2 Gaia

Gaia
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationCanada

Posted 12 April 2006 - 03:54 PM

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.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 April 2006 - 04:00 PM

if you're wanting to simply know HOW it's done, take a look at the code snippet below for a very basic example:

<?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;
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 redneonrt

redneonrt
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 12 April 2006 - 04:17 PM

[!--quoteo(post=364089:date=Apr 12 2006, 11:00 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Apr 12 2006, 11:00 AM) View Post[/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?


#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 April 2006 - 04:22 PM

[!--quoteo(post=364096:date=Apr 12 2006, 12:17 PM:name=redneonrt)--][div class=\'quotetop\']QUOTE(redneonrt @ Apr 12 2006, 12:17 PM) View Post[/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:

$String = file_get_contents('myTemplate.tpl');

$replace = array("GREETING" => "Hello!", "USER" => "obsidian");

$String = replace_all($String, $replace);
echo $String;

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 redneonrt

redneonrt
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 12 April 2006 - 04:55 PM

Thats great,

Thanks again everyone!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users