Jump to content

Easy Template system for Any website


Archadian28

Recommended Posts

Well i said i would post a template system so here it is:

 

My Directory:

 


classes

 templates.php

images (for the website of course nothing to do with templates ).

templates (directory)
  footer.html
  header.html
  index.html   
  whatever.html

index.php

 

In the classes folder is the templates class file: templates.php:

 


<?php

class Template {

var $file;
var $incfile;
var $assign = array();
protected $tplex = ".html";
protected $tmpdir = 'templates/';

function load($filename) {

	$file = $this->tmpdir . $filename . $this->tplex;

	if (file_exists($file)) {

		$this->template = file_get_contents($file);

	}
	else {

                   // Your error message

	}

}

function get_contents($contents) {

	$this->template = file_get_contents($contents);

}

function inc($incfile) {

	$file = $this->tmpdir . $incfile . $this->tplex;

	include $file;	

}

function showPage() {

	foreach($this->assign as $key => $val) {

		$this->template = str_replace("{" . $key . "}", $val, $this->template);

	}

	eval("?>" . $this->template);

}

} // end class

?>

 

Now in the templates directory you name your files whatever you want...the MAIN PAGE when they go to your URL HAS to be named index.html or .whatever but make sure you change it from .html to whatever extension you want to use and ALL files in the templates folder MUST have the same extension. Change the extension here:

 

protected $tplex = ".html";

And in the index.php: (below)

$chkpg = templates/ . $page . ".html";

 

this is my index.php file to show the entire website...DO NOT change anything in this or it will not work.

 


// Include the template.php class file in index.php

$template = new Template();

// Include header.html
$template->inc('header');

// Get the page. If its not set then set it to index.
$page = (isset($_GET['page'])) ? htmlentities(addslashes(trim($_GET['page']))) : 'index';
unset($_GET['page']);
// Check to make sure the page actually exists on the webserver. If not, take your ass to the homepage...hacker.
$chkpg = TEMP_PATH . $page . ".html";

if (!file_exists($chkpg)) {

   // Error message here if the file in the templates directory does not exist. This keeps this system secure for the most part. don't forget to redirect to the homepage
}

if(file_exists($chkpg) && isset($page)) {

   $template->load($page);

}
else {

   $template->load('index');

}

$template->showPage();

$template->inc('footer');

}

 

now the $template->inc('header') includes this: (VERY important) This is named header.html in my templates folder

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta name='keywords' content=''>
<meta name='description' content=''>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />

<title>YOUR TITLE</title>

//INSERT any CSS external links to pages or javascript here

</head>

<body>

// Top logo or banner here

// Navigation here if your navigation is going across the top.

<div>

// this is where $template-load($page) displays from index.php

 

and my footer.html: (also very important)

 



   <div id="footer">

       <p class="content">All images and content belongs to IMAGINATION UNLEASH3D Copyright © 2011-2012. All rights reserved.</p>

  </div>

</div> // Div close after body in header.html

</body>
</html>


 

So what that does if you look at the index.php any file you create in your templates folder besides the header and footer.html will show up between those two to complete your website. So index.html will be your main page and you might have another file named contactus.html or something like that. ALL your navigation and logo banners go in the header.html the only thing that needs its own page is like the homepage or contact us page or about us page, etc. and that can be done with a simple <div></div>. Now to link to these pages you simply add this to the URL:

 

?page=contacts

 

?page=contacts would be contacts.html in your templates folder (not ?page=contacts.html)

 

Here is an example of mine:

 


<div id="nav">

   <ul id="buttons">
     <li class="home"><a href="index.php"></a></li>
     <li class="portfolio"><a href="index.php?page=portfolio"></a></li>
     <li class="reviews"><a href="index.php?page=reviews"></a></li>
     <li class="aboutus"><a href="index.php?page=aboutus"></a></li>
     <li class="contact"><a href="index.php?page=contact"></a></li>
   </ul>

</div> <!-- Nav End -->

 

Pay attention to everything in the <a href=""></a> tags. Thats how you link to your pages. If you have any questions just post and i will answer them. Enjoy!.

 

You MUST include the header.html and footer.html with the index.html. All template files go in the templates directory.

 

What this does is run everything through the index.php file so if you have forms ANYWHERE in your website you can simply add action="index.php" to any form and simply get the $_POST or $_GET on the index.php file...i don't recommend this. Make a function file for this and call the function on index.php file. If you have any questions just ask!

Edited by Archadian
Link to comment
Share on other sites

For added security put this at the top of index.php:

 


define('WHATEVER_HERE', true); // Change WHATEVER_HERE to something else...

 

then at the top of every page in templates add this (must be the first thing on the page!..besides session_start();):

 


<?php

if (!defined('WHATEVER_HERE')) {

header("Location: index.php"); // You can log IPs, redirect to the homepage or send them to a nice error page. 

}
?>

 

What this does is something is defined on index.php and once you add it to every page in the templates directory they can't just go to http://www.yoursite.com'>http://www.yoursite.com /templates/ index.html...they have to load the index.php file by going to http://www.yoursite.com. Not sure if this can be bypassed or not but i've tested ever route possible but without loading the index.php in the web directory first i was not able to load anything in the templates directory. Also files with just images or anything that normally would not have an index.html or index.php file in that directory you can create a file with just the code i posted above with the IF statement so if they go to http://www.yoursite.com /images/ your whole directory will not be visible it will redirect them to the homepage...you may have to add ../ infront of index.php. This is in response to my other post about security. If you aren't familiar with securing your website google has many tutorials that can help you. Security is #1 :)

Edited by Archadian
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...