holdorfold Posted September 27, 2014 Share Posted September 27, 2014 (edited) I've made a test database from the book "PHP and mySQL Novice to Ninja"... now I'm testing out making the structure better by using templates to output the html. This is the template... note the part in with the arrow pointing to it. That's the dynamic content which should change based upon what page is passed to it. <!DOCTYPE html> <html lang="en"> <head> <link href= "/project/includes/style.css" rel="stylesheet" type="text/css"> <meta charset="utf-8"> <title>Joke Database Project</title> </head> <body> <div id="topheader"> <p>Joke Database Project</p> </div> <div id="navigation"> <?php include $_SERVER['DOCUMENT_ROOT'] .'/project/includes/navbar.html.php'; ?> </div> <div id="container"> <?php include '/'.$content;?> <<<<--------------------------------------- </div> </body> </html> It works fine when I use the following code in my php script: $content="authors.html.php"; include $_SERVER['DOCUMENT_ROOT'] . '/project/includes/html_template.html.php'; However I want to make it less clunky so I decided to create the following function include in my php script so I can show the relevant content by using showpage("author.html.php") function showpage($content) { include $_SERVER['DOCUMENT_ROOT'] . '/project/includes/html_template.html.php'; } The problem I'm having is when I use this function the outputed page no longer recognises the variables from the main script so the proper content doesn't show. (note it works fine when not using the function but just including it directly in the script). How can I get around this in an elegant way? Edited September 27, 2014 by holdorfold Quote Link to comment https://forums.phpfreaks.com/topic/291319-how-to-load-pages-within-a-template-properly/ Share on other sites More sharing options...
Frank_b Posted September 28, 2014 Share Posted September 28, 2014 (edited) Give an array as the second parameter to showpage() $var = 'This is a variable'; $data = array( 'var' => $var, // more variables ); showpage("author.html.php", $data); Then into the showpage function use extract() to make the associative array as variables: function showpage($templateName, $data) { extract($data); unset($data); // if you wish include '/bla/bla/'.$templateName; } The template could be something like this: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php echo $var; ?> </body> </html> Edited September 28, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/291319-how-to-load-pages-within-a-template-properly/#findComment-1492255 Share on other sites More sharing options...
trq Posted September 28, 2014 Share Posted September 28, 2014 Instead of re-inventing your own templating system, why not use one that already exists? You could start with something simple like Plates (which is just a few simple helpers) or a complete templating engine like Twig (which requires you learn a new syntax). Either way, people have already resolved the issues that you are running into, in far more elegant ways. 1 Quote Link to comment https://forums.phpfreaks.com/topic/291319-how-to-load-pages-within-a-template-properly/#findComment-1492258 Share on other sites More sharing options...
holdorfold Posted September 28, 2014 Author Share Posted September 28, 2014 Both great answers, thanks a ton! Quote Link to comment https://forums.phpfreaks.com/topic/291319-how-to-load-pages-within-a-template-properly/#findComment-1492265 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.