Jump to content

Using a HTML template with includes can't read from $_GET


rpmorrow

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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 ?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.