-1

So I know there are plenty of engines all ready out there like smarty that will do this for me but I want to build a basic one just for my own learning rather than just jumping on someone else's code for now.

So far I have a set of pages (header.php, footer.php, rightPanel.php, home.php) all of which are loaded by a function.

<?php
require_once(realpath(dirname(__FILE__) . "/../config.php"));

function renderLayoutWithContentFile($contentFile, $variables = array())
{
    $contentFileFullPath = TEMPLATES_PATH . "/" . $contentFile;

    // making sure passed in variables are in scope of the template
    // each key in the $variables array will become a variable
    if (count($variables) > 0) {
        foreach ($variables as $key => $value) {
            if (strlen($key) > 0) {
                ${$key} = $value;
            }
        }
    }

    require_once(TEMPLATES_PATH . "/header.php");

    echo "<div id=\"container\">\n"
       . "\t<div id=\"content\">\n";

    if (file_exists($contentFileFullPath)) {
        require_once($contentFileFullPath);
    } else {
        require_once(TEMPLATES_PATH . "/error.php");
    }

    // close content div
    echo "\t</div>\n";

    require_once(TEMPLATES_PATH . "/rightPanel.php");

    // close container div
    echo "</div>\n";

    require_once(TEMPLATES_PATH . "/footer.php");
}
?>

in index.php

<?php

require_once(realpath(dirname(__FILE__) . "/resources/config.php"));

require_once(LIBRARY_PATH . "/templateFunctions.php");
require_once(LIBRARY_PATH . "/dealBuilderFunctions.php");

$setInIndexDotPhp = "Hey! I was set in the index.php file.";

// Must pass in variables (as an array) to use in template
$variables = array(
    'setInIndexDotPhp' => $setInIndexDotPhp,
);

renderLayoutWithContentFile("home.php", $variables);

?>

so now what if I want to load a different set of content into the template, I have figured I can do this to change the variable in the template page and input some logic to get it to check for a change like below:

<?php

require_once(realpath(dirname(__FILE__) . "/resources/config.php"));

require_once(LIBRARY_PATH . "/templateFunctions.php");
require_once(LIBRARY_PATH . "/dealBuilderFunctions.php");

/*
    Now you can handle all your php logic outside of the template
    file which makes for very clean code!
*/

$setInIndexDotPhp = "Hey! I was set in the index.php file.";

// Must pass in variables (as an array) to use in template
$variables = array(
    'setInIndexDotPhp' => $setInIndexDotPhp,
);

if ($page == "") {
    $page = "home";
} 
else {
    $page=$page;
}
renderLayoutWithContentFile("$page.php", $variables);

?>

URL to get to new page

http://siteurl.com/index.php?page=test

but I keep getting an error Notice: Undefined variable: page in /www/sites/perthdeals/wwwroot/index.php on line 20, how can I get this to work?

I thought the above should just change any variable $page in index.php but if I set it in the page like the error asks I just get the page I defined inside the code, not the one passed in URL, so how should it work?

Alternatively if there are any modern tutorials that will help me understand this I would appreciate it, Thanks.

UPDATE - The Undefined variable: page in /www/sites/perthdeals/wwwroot/index.php on line 20 was a red herring, I was trying to call the variable from the URL wrong, answer marked below, thanks.

CKelly-Cook
  • 195
  • 3
  • 10
  • update added, the error wasn't the actual problem, while the proposed duplicate solution got rid of the error it did not fix the actual issue, answer marked below. – CKelly-Cook Apr 08 '15 at 07:28

1 Answers1

0

parameters from the URL you can get through $_GET, in your case

if ($_GET["page"] == "") {
    $page = "home";
} 
else {
    $page=$_GET["page"];
}
zdeniiik
  • 222
  • 3
  • 14
  • 1
    Thanks, I was still showing the error even though it was working when I set page in the URL so I changed the code to below based on the other article ` if (isset($_GET["page"])) { $page=$_GET["page"]; } else { $page = "home"; }` – CKelly-Cook Apr 08 '15 at 07:34