Helpful Information
 
 
Category: Coding tips & tutorials threads
Making PHP Templates

Hi there,

I'm here to show you today how to make PHP templates! PHP templates are much easier to manage in real world applications, and are extremely easy to make. Let's begin.

Estimated time: 10 minutes if you're starting fresh, and about roughly 5 minutes per page if you're converting.
Skills you need: Very basic PHP knowledge, some HTML, and the ability to delete code.

We'll start off with the basic URL structure.

Right now, I'll assume that you have something like this:


http://www.mydomain.com/mypage.php
http://www.mydomain.com/anotherpage.php

After this tutorial, you'll end up with something like this:


http://www.mydomain.com/index.php?p=mypage
http://www.mydomain.com/index.php?p=anotherpage

So, firstly, strip out all your html code EXCEPT for the code between the <body></body> tags in all your pages except the index page (or where you want your "master page" to be) and put all your files (again, except the master page) in one folder. We'll use "inc" as an example.

And then... place this code where you want your content to be.



<?php
$default = 'home'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.
$page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page.
$page = basename($page); //Gets the page name only, and no directories.
if (!file_exists('inc/'.$page.'.php')) { //Checks if the file doesn't exist
$page = $default; //If it doesn't, it'll revert back to the default page
//NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.
}
include('inc/'.$page.'.php'); //And now it's on your page!
?>

Alrighty, we've now got the hard part done.

Now, if you're converting your pages into this template format, you'll need to modify your menu links. Trust me, it's worth it!

Say you have your menu code like this,


<ul>
<li><a href="mypage.php">My Page</a></li>
<li><a href="someotherpage.php">Some Other Page</a></li>
</ul>

You'll need to change it to this:


<ul>
<li><a href="?p=mypage">My Page</a></li>
<li><a href="?p=someotherpage">Some Other Page</a></li>
</ul>

...in order for it to work.

Now, if you want to get a little bit more complex, you can make a PHP function for it to generate the menu for you!

That way, you can add menu items easily and even change the window title!

You'll need to make some modifications to the original PHP code in order for this to work.

Firstly, replace your whole menu code with this:

<?php generateMenu(); ?>

And somewhere on your page, or on another page place this:
(Make sure that it is BEFORE your content grabber snippet (The first PHP code on this tutorial)


<?php
$menu = array();
$menu['home'] = 'Home';
$menu['mypage'] = 'My Page';
//Add in the format of: $menu['page name'] = 'Page Title';
$title='Home'; //Default title
function generateMenu() {
global $menu,$default,$title;
echo ' <ul>';
$p = isset($_GET['p']) ? $_GET['p'] : $default;
foreach ($menu as $link=>$item) {
$class='';
if ($link==$p) {
$class=' class="selected"';
$title=$item;
}
echo '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>';
}
echo '</ul>';
}
?>
Change accordingly.

And on your document title, place:

<?php echo $title; ?>
Wherever you want the title of the page to be.

Here's an example of the title:

<title>My PHP Template - <?php echo $title; ?></title>

Ok, that's the end of this tutorial. If you have any questions, feel free to start a thread on it. :)

For those who don't have time to read this whole thing, here's the whole PHP code:



<?php
$menu = array();
$menu['home'] = 'Home';
$menu['mypage'] = 'My Page';
//Add in the format of: $menu['page name'] = 'Page Title';
$title='Home'; //Default title
function generateMenu() {
global $menu,$default,$title;
echo ' <ul>';
$p = isset($_GET['p']) ? $_GET['p'] : $default;
foreach ($menu as $link=>$item) {
$class='';
if ($link==$p) {
$class=' class="selected"';
$title=$item;
}
echo '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>';
}
echo '</ul>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My PHP Template - <?php echo $title; ?></title>
</head>

<body>
<?php
generateMenu();
$default = 'home'; //Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.
$page = isset($_GET['p']) ? $_GET['p'] : $default; //Checks if ?p is set, and puts the page in and if not, it goes to the default page.
if (!file_exists('inc/'.$page.'.php')) { //Checks if the file doesn't exist
$page = $default; //If it doesn't, it'll revert back to the default page
//NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.
}
include('inc/'.$page.'.php'); //And now it's on your page!
?>
</body>
</html>


Added basename($page) for extra security

This is a very basic way of doing templates. It may work for small applications, but it's not as powerful as a real full-sized templating engine such as Smarty (http://smarty.php.net/).










privacy (GDPR)