[SOLVED] {template variable} -> include file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

[SOLVED] {template variable} -> include file

Post by The Monkey »

Hello there,

I am, yes, still working on my site redesign. I am planning for it to be done in one week!

However, I decided to go with a templating system. I now have header.php and footer.php, and a seperate script replaces all of the varying variables, such as:
{title}
{randQuote}
{etc}

with the appropriate PHP Variable.

However, my menu is dynamic, with a quite elaborate script. And the way the templating system is set up, I can not simply put an include call in header and/or footer.php.

What I need to do is have a seperate script for the menu, e.g., menu.php. When the templating system finds {menu} in header.php, it needs to replace that call with the output generated by menu.php... do you understand?

I hope I have made it clear enough.

If you do not understand, please state as such and I will do my best to clarify.

- The Monkey
Last edited by The Monkey on Fri Jun 25, 2004 12:24 pm, edited 1 time in total.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

basically you pass a class an array of the things to be replaced

Code: Select all

$vars = array('TITLE' => 'hi',
'NAME' => 'PrObLeM',
'FOO' => 'BAR!!')

//then
//you parse the variable with the data
foreach ($tags as $tag => $data) 
	{
	     $this->page = eregi_replace('{' . $tag . '}', $data, $this->page);
	}
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

for menu it would be:

Code: Select all

ob_start();
include 'menu.php';
$menu_txt = ob_get_contents();
ob_end_clean();
$vars = array(
    'TITLE' => 'hi', 
    'NAME' => 'PrObLeM', 
    'FOO' => 'BAR!!',
    'MENU' => $menu_txt
); 

//then 
//you parse the variable with the data 
foreach ($tags as $tag => $data) 
   { 
        $this->page = eregi_replace('{' . $tag . '}', $data, $this->page); 
   }
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

pass the menu to this function:

Code: Select all

function parse($file){
		ob_start();
		include($file);
		$buffer = ob_get_contents();
		ob_end_clean();
		return $buffer;
	}
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

Thank you, that works great. The ob_start() and ob_end_clean() is exactly what I needed. That command will come in handy in many places.

- Monkey
Post Reply