Jump to content

Recommended Posts

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 by holdorfold

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 by Frank_b

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.

  • Like 1
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.