Forum Moderators: coopster
I know that you would normally use;
include("script.php");
but my question is slightly different....
what i have is a template system that uses an html template - which uses the search and replace function to replace "<% tags %>" with the "$content" - where $content varies from page to page, and is included on the appropriate page. What I am wanting to do is include script.php into the $content variable - but not sure how to.
I have tried searching around, and came up with some javascript equivalent <script language="javascript" src="script.php"></script> - but couldnt get this to work.
Basically - script.php contains more variables, and php scripting, as well as html to go on the output - a refer to a friend script.
basically the script putting template + content together is like this - there are other variables in here - but i left just 1 example in there, content - which is where i want the content to appear.
<?php
$content = 'content html stuff goes here';
function template() {
global $page_content;
$filename = 'template.html';
if(!$fd = fopen($filename, "r")) {
$error = 1;
}
else {
$template = fread ($fd, filesize ($filename));
fclose ($fd);
$template = stripslashes($template);
$template = eregi_replace("<% page_content %>", "$content", $template);
echo "$template";
}
}
function home() {
global $page_content;
template("$data");
}
switch($action) {
default:
home();
break;
}
?>
And basically on the html page is just the tag <% page_content %> which the script replaces with the content of the variable $content.
If anyone could help - this would be great.
PHP has a function that allows you to Evaluate a string as PHP code [php.net], but I'm not sure what you are attempting to do is the best design. Be very careful you aren't getting yourself into an endless loop.
You are after a system that includes a file, based on a variable?
eg:
if $content == "home", you want it to include "home.php"?
If so, its truly very simple!
The only things you have to consider, is the possibilites of abusing the include, to include sensitive information.. (/etc/passwd, anyone?)
You can "control" the includes, either by searching the include variable, or you can define which pages are allowed to be included.
There are a million ways of doing this, you could either:
define an array, with pages..
$page['Home'] = "includes/home.php";
then you do:
include($page[$querystring_variable]); (here querystring would be "Home")
Or, you can define it as integers:
$page[0] = "includes/home.php";
then you do:
include($page[$querystring_variable]); (here querystring would be 0 (the integer 0))
Or, you could:
include($querystring_variable . ".php");
The last example here, would be limited in filetype, as well as you would need to check if it includes slashes, etc. to prevent abuse. You would also have to check that it is not trying to include it self, so you wont have an infinite loop of includes!
If you want more, I can provide an working example of the last one here, but I guess you get the general idea.
o - and it appears above the html, head, body tags etc etc
Well, why are you including it outside of your html then?
You can simply include it *anywhere* in the document!
eg. if you have a div for content:
<div class="content" id="content" title="content">
<!--#Content-->
<?php
// your code goes here
?>
</div>
ps. if you already did include it inside the content, check the included file for html errors.. (some tags may not be closed, etc.)
... as mentioned above, i am using a search and replace method for my html template page - and the php script finds some <% tags %> and replaces it with the nessisary $variable strings - i just want to replace one of this $variables with content from another php page, as explained above - using the find replace method at the very above
ps. your problem might be the %, as % in co-operation with some characters, may mean something else..
eg. %20 = space (" ")
you could try to replace <!--#content--> with the $content.
but this is no include, by using your method, you would first read thru the entire file and then replace it.. or maybe you get the text from db?
if you get it from DB, you simply parse it as wanted.