rpmorrow Posted April 3, 2011 Share Posted April 3, 2011 I am trying to make a site with content in databases and using some template html files for the layout. The problem I am having is that the code is executing in the template directory and can't therefore read the value from any $_GET that is on the url used by the user. I hope you can understand my meaning. Let me give an example of the structure.. The user visits "www.example.com/search.php?q=something" The file "search .php" just contains a function call to display the page; e.g. DisplayPage("search"); The DisplayPage function uses CURL to read the contents of a HTML file (/templates/search-template.htm) into a string. It then uses preg_replace and str_replace to replace some markers that are in the template, with data from the database. At the end of the function it simply echoes the string. The template files contain some includes, which work fine, but the code executing within them "thinks" it is being executed from /templates/search-template.htm rather than /search.php. Therefore if I use $_GET['q'] in any of those includes, it is empty. I hope that makes sense! Is there any way for me to make the executing code work as if it is being executed from "/search.php" rather than the template? Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/ Share on other sites More sharing options...
harristweed Posted April 3, 2011 Share Posted April 3, 2011 You can not parse php in a page with an HTML extension. Save the page with a PHP extension and try that! Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196153 Share on other sites More sharing options...
rpmorrow Posted April 3, 2011 Author Share Posted April 3, 2011 Yes you can! Just adding "AddType x-mapp-php5 .html .htm" to the .htaccess file does it. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196157 Share on other sites More sharing options...
rpmorrow Posted April 3, 2011 Author Share Posted April 3, 2011 So.. any ideas..? :'( Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196212 Share on other sites More sharing options...
j9sjam3 Posted April 3, 2011 Share Posted April 3, 2011 Can't you use .htaccess to rewrite the URL's? e.g. File: search.php?q=searchterm Will be seen in URL as: search/searchterm/ Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196213 Share on other sites More sharing options...
rpmorrow Posted April 3, 2011 Author Share Posted April 3, 2011 Maybe, but I don't see how that solves my problem. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196214 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2011 Share Posted April 3, 2011 The DisplayPage function uses CURL to read the contents of a HTML... ^^^ That's making a separate HTTP request back to your server to request the file, so it is not search.php where the included code is executing at, it is search-template.htm where the included code is executing at. Why would you use CURL to do this? That would be extremely slow compared to using the file system to include the template. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196227 Share on other sites More sharing options...
rpmorrow Posted April 3, 2011 Author Share Posted April 3, 2011 I am using curl because I need to str_replace data in the template. I didn't know it would be slow.. hmm, maybe i should look at another approach for this Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196234 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2011 Share Posted April 3, 2011 You would use file_get_contents() with a file system path to read the contents of the template into a variable. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196235 Share on other sites More sharing options...
rpmorrow Posted April 3, 2011 Author Share Posted April 3, 2011 Thanks, I had tried using that but the includes in the template stop working. I'm calling them in the template as follows.. <? include_once($_SERVER['DOCUMENT_ROOT'].'/includes/menu-top.inc.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196240 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 I *think* if you use output buffering you could somehow get this to work. For example, call ob_start() right before include()ing the html file. Then, call ob_get_clean() to get what the included file outputted and clean the buffer so it doesn't output to the browser. ob_start(); include('somehtmlwithphpcode.html'); $output = ob_get_clean(); That'll let the file actually execute and $_GET stays the same. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196251 Share on other sites More sharing options...
PFMaBiSmAd Posted April 3, 2011 Share Posted April 3, 2011 To get the quickest solution, you would need to post a sample of your code that demonstrates/reproduces what you are doing and shows what does not work. A template should be just that - "a pattern, used as a guide in making something." A template should not be doing anything itself. Your main code should be producing the content that goes into the template, replaces your placeholders in the template with the appropriate content, and output the results. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196260 Share on other sites More sharing options...
rpmorrow Posted April 3, 2011 Author Share Posted April 3, 2011 If I'm using file_get_contents(), it does not parse the PHP code, so when I go view-source, it looks as below. I've put the various code segments below. Example of template code (I've removed all the html header etc for simplicity): <div id="content"> <div id="leftColumn"> ~~LEFTMENU~~ <? include_once($_SERVER['DOCUMENT_ROOT'].'/includes/box-left.inc.php'); ?> </div> <div id="rightColumn"> <? include_once($_SERVER['DOCUMENT_ROOT'].'/includes/box-right.inc.php'); ?> </div> ~~PAGECONTENT~~ <? include_once($_SERVER['DOCUMENT_ROOT'].'/includes/searchresult.inc.php'); ?> </div> <div id="footer"> <? include_once($_SERVER['DOCUMENT_ROOT'].'/includes/footer.inc.php'); ?> </div> /includes/searchresult.inc.php <? $searchQuery = $_GET['q']; echo $searchQuery; //was empty ?> The code to replace ~~PAGECONTENT~~ etc is in functions.inc.php (simplified) <? function DisplayPage($page_id, $page_type) { //the two variables here are used to select the right info from database, and which template to use $template_html = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/templates/home-template.htm'); $parsed_html = preg_replace("/~~PAGECONTENT~~/",$row['page_content'],$template_html,1); echo $parsed_html; ?> The file visited by the user www.example.com/search.php?q=something <? include_once('includes/functions.inc.php'); DisplayPage("Search", "search"); //page_id, page_type ?> P.S. I'm using the code button in the forum, but it is not colouring the php for u, I need add something else? Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196275 Share on other sites More sharing options...
dcro2 Posted April 3, 2011 Share Posted April 3, 2011 Try using the method I posted. Like this for your DisplayPage function: <?php function DisplayPage($page_id, $page_type) { //the two variables here are used to select the right info from database, and which template to use ob_start(); include_once($_SERVER['DOCUMENT_ROOT'].'/templates/home-template.htm'); $output = preg_replace("/~~PAGECONTENT~~/",$row['page_content'],ob_get_clean(),1); echo $output; ?> Worked perfectly for me. Use [ php ] for php code btw. [ code ] doesn't color php code unless you wrap it in <?php ?> Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196318 Share on other sites More sharing options...
rpmorrow Posted April 4, 2011 Author Share Posted April 4, 2011 Cool, that seems to be working! $_GET is working too!! To do the replacing repeatedly, I'm doing the following, its working but it feels so strange to do it like this (I never even heard of buffering the output like this b4 now). Is it ok? $output = preg_replace("/~~FIRSTTHING~~/",$row['first'],ob_get_clean(),1); $output = preg_replace("/~~SECONDTHING~~/",$row['second'],$output,1); $output = preg_replace("/~~THIRDTHING~~/",$row['third'],$output,1); echo $output; $output = preg_replace("/~~LASTTHING~~/",$row['last'],ob_get_clean(),1); echo $output; Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196422 Share on other sites More sharing options...
dcro2 Posted April 4, 2011 Share Posted April 4, 2011 If you want to do more than one replacement with preg_replace you can plug arrays into it and do it all in one go. So you could do this instead: $patterns = array("/~~FIRSTTHING~~/", "/~~SECONDTHING~~/"); //and so on... $replacements = array($row['first'], $row['second']); //and so on... $output = preg_replace($patterns, $replacements, ob_get_clean(), 1); I've never actually used output buffering this way, but today as I was looking for other things I found others using even hackier methods. If the functions are there, why not? It would make more sense to do it all by replacements though, like templates were meant to be. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196444 Share on other sites More sharing options...
rpmorrow Posted April 4, 2011 Author Share Posted April 4, 2011 Thank you very much. It's all working great and my code is much simpler I had a look around about the buffering, it all looks quite interesting. I'm sure I will be using it more in future. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/232546-using-a-html-template-with-includes-cant-read-from-_get/#findComment-1196454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.