Page 1 of 1

[SOLVED] {template variable} -> include file

Posted: Fri Jun 25, 2004 8:30 am
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

Posted: Fri Jun 25, 2004 10:25 am
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);
	}

Posted: Fri Jun 25, 2004 10:55 am
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); 
   }

Posted: Fri Jun 25, 2004 11:11 am
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;
	}

Posted: Fri Jun 25, 2004 12:23 pm
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