Log in

View Full Version : turn variable in eregi_replace string into incl file



?foru
10-15-2008, 04:47 AM
I've figured out another way to add modules to a CMS system, but I would like to have the possibility of adding additonal features to the template system such as included files.

The physical "theme.php" page uses <% main %> and the index page builds the template like this.


$template = eregi_replace("<% main %>", "$main", $template);

I would like to be able to turn $main into an included file, and I searched and found the following...


<?php
ob_start(); # start buffer
include_once( 'path/to/included/file/file.php' );
# we pass the output to a variable
$main = ob_get_contents();
ob_end_clean(); # end buffer
# and here's our variable filled up with the html
echo $main;

//....code to start to prepare the template
$template = eregi_replace("<% main %>", "$main", $template);
//...code to finish the template

?>

The issue is that I can't use echo $main; like above because it prints out at that exact position above the template. Anyone have an idea on how I can turn the $main variable in the eregi_replace string into the included page so <% main %> in the "template" file is replaced with the included file?

Thank you

Jesdisciple
10-15-2008, 10:19 PM
I don't understand... You mean like this?
<?php
ob_start(); # start buffer
include_once( 'path/to/included/file/file.php' );
# we pass the output to a variable
$main = ob_get_contents();
ob_end_clean(); # end buffer

//....code to start to prepare the template
$template = eregi_replace("<% main %>", "$main", $template);
//...code to finish the template

?> BTW, if file.php returns its output rather than echoing it, you can do this:
$main = include('path/to/included/file/file.php');
$template = eregi_replace("<% main %>", "$main", $template);

?foru
10-17-2008, 02:20 AM
Thank you for your reply. I ran some tests since I first saw your post and did get this to work.

From what I can tell this CMS is no longer available, but the template system may have been a little difficult to work with for others also. I found somewhere that someone had changed the template system with the following and it seemed to work a little better.

Seperate template class file is included and the template is now structured like...(class changes first part to <% main %> so it works in the template.

$template->__replace("main",$main);

Using the new template system I tried the following...
This worked to include the file, but for whatever reason even though it was right above the $template line like in your example it didn't place it at the <% main %> location in the template.

$main = include('path/to/included/file/file.php');

When I used this it worked to add that content into the specific spot in the template.

ob_start(); # start buffer
include_once( 'path/to/included/file/file.php' );
# we pass the output to a variable
$main = ob_get_contents();
ob_end_clean(); # end buffer

I was hoping to just be able to include files like normal into the template because this is all on a php server and all pages are php extension including the "template" file. I guess it didn't work that way since it works on this "replace" type template system where everything needs to be defined.

Jesdisciple
10-17-2008, 02:33 AM
I take it you fixed everything?

To clarify, the difference between these two patterns is in the included file:

<?php
ob_start();
include('path/to/included/file.php');
$contents = ob_get_contents();
ob_end_clean();
?>

<?php
$contents = include('path/to/included/file.php');
?>
Here are the corresponding include files:

<?php
echo 'Put ';
echo 'your ';
echo 'contents ';
echo 'here.';
?>

<?php
$output = 'Put ';
$output .= 'your ';
$output .= 'contents ';
$output .= 'here.';
return output;
?>

?foru
10-18-2008, 02:24 AM
Yes, I did get it to work. I appreciate your clarification on these 2 types of outputs, and so I could see it firsthand I tested the code you provided.

I had to resort to this because I believe the way the template is compiled I was unable to successfully include php files directly into the template in the standard way like below.
<?php include('file.php'); ?>