Trying to create templates?

I’m trying to make an site / cms in which the user of it can choose between different design templates. The issue is that when doing it like I try below, I can’t use “echo” on my sites… How do I get passed this?

index.php

$sitetitle = $clubname;
$pagetitle = "Startpage";
$page = 'test'; 

if(file_exists('pages/'.$page.'.php')){ 

	$main .= include('pages/'.$page.'.php'); // Here is the problem.
	
 } else { 
 
	$main .= "<br><br><br><br><br><center>
		Sorry!<br><br>Page is down for now!<br><br></center>";
	
 } 

require('templates/'.$usertemplate.'/header.php');
require('templates/'.$usertemplate.'/mainmenu.php');
require('templates/'.$usertemplate.'/footer.php');
 
// First we read the contents of the template.php file as the variable $template.
$template = file_get_contents('templates/'.$usertemplate.'/template.php');

// Now we do a search and replace on $template inserting content where the keywords were placed
// We do this with 2 arrays, one for search and one for replace.
// We then use the preg_replace function to make the changes and create an output variable

$search = array('|{PAGETITLE}|',
'|{MAINMENU}|',
'|{MAIN}|',
'|{HEADER}|',
'|{FOOTER}|'
);

$replace = array("$sitetitle - $pagetitle", 
$main_menu,
$main,
$header,
$footer
);

$output = preg_replace($search, $replace, $template);

echo $output;

In the above I reference $main to an file called page.php. In this file I would like to be able to use “echo” in this page…

test.php

echo 'This is a test';

And the page that bindes all together:

template.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{PAGETITLE}</title>
<link rel="stylesheet" href="templates/standard/css/mainstyle.css" type="text/css" media="screen">
</head>

<body> 
<div id="container">
	
    {HEADER}

    {MAINMENU} 
    
    {MAIN}

    {FOOTER}

</div>
</body>
</html>

When using “echo” the {MAIN} comes before anything else… How can I solve this?

Thanks in advance :wink:

Is there a reason you’re not using a templating library that already exists and offers the functionality you’re after?

Nope… Can you name some?

Well actually I only want to be able to change skins…

Smarty is the best and easiest one i have found and used, you can get it from PHP Template Engine | Smarty

Twig. No competition. :slight_smile:

Thanks for the share Anthony, looks a lot more powerful then Smarty :slight_smile:

have you tried using return instead of echo then using echo when the template has been built


$sitetitle = $clubname;

$pagetitle = "Startpage";

$page = 'test'; 



if(file_exists('pages/'.$page.'.php')){ 


    ob_start();
    include('pages/'.$page.'.php'); // Here is the problem.
    $main = ob_get_contents();
    ob_end_clean();

    

 } else { 

 

    $main .= "<br><br><br><br><br><center>

        Sorry!<br><br>Page is down for now!<br><br></center>";

    

 } 



require('templates/'.$usertemplate.'/header.php');

require('templates/'.$usertemplate.'/mainmenu.php');

require('templates/'.$usertemplate.'/footer.php');

 

// First we read the contents of the template.php file as the variable $template.

$template = file_get_contents('templates/'.$usertemplate.'/template.php');



// Now we do a search and replace on $template inserting content where the keywords were placed

// We do this with 2 arrays, one for search and one for replace.

// We then use the preg_replace function to make the changes and create an output variable



$search = array('|{PAGETITLE}|',

'|{MAINMENU}|',

'|{MAIN}|',

'|{HEADER}|',

'|{FOOTER}|'

);



$replace = array("$sitetitle - $pagetitle", 

$main_menu,

$main,

$header,

$footer

);



$output = preg_replace($search, $replace, $template);



echo $output;