Scrigroup - Documente si articole


HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Making A Basic Templating System


+ Font mai mare | - Font mai mic

Making A Basic Templating System 

One of the most asked questions I get, is 'How do I make a basic templating system in php?'  Okay, well it's not.  Actually the most common questions I get is, 'Can you make a basic template system for me?'

While I have no problems helping people, and slaving for them all day long, there comes a time when you must teach a person to fish, rather than always catching the fis for them.

So here Goes

A One page template system

Now all though this is called a 'one page' template system, it really doesn't use just one page.  It uses a total of 3.  The reason it's called a 'One Page' system, is because 'one page' does all of the work.  The files you will be creating are as follows:

  • index.php
  • template.html
  • content.php

You've guessed right, if you figured that content.php will be where all of your content is stored, template.html is your template, and index.php is the PHP script that puts them all together, and runs your site.

Now, there are many types of template systems.  However, they all have the same concept, and pretty much work the same.  The difference is some template systems use a large number of pages, for each content portion of your site.  Since that system still leaves you to create a new page for each portion of your content, it's probably not as efficient.  So, for this article I'll stick with the One Page System.

So let's get started!

Buliding Your Template File

The first file that should be created is your template file: template.html

So let's open your favorite text editor.  I prefer CrimsonEditor personally.

In any case, open your favorite text editor, and create a new blank document.  Place the following code in your new document:

<table cellpadding='0' spacepadding='0' border='0'>
<tr><td valign=top align=left>
<% maincontent %>

The above is just a basic template. Your template can look however you want it to, as long as it contains the tag <% maincontent %>

where you want the content to appear in your pages.

Save the above page as template.html

Creating your index.php Script

Now, of course, we need to create the functioning portion of your site. So with the same trusty text editor, create a new document and place the following code in it:

function template($content)
        else }

function cmd()


Save the above document as index.php, and let's move on.
Create Your content.php file

Now this file will contain all of your content, and will serve the content, along with your template file, into your index.php file, conditionally based

on the variables passed in the URL.

Create a new document, and add the following code. Hopefully you will see the pattern in this document, that will allow you to keep adding

content to it, and break it up into content sections:


if($go == '')

if($go == 'prices')

if($go == 'contact')


Save the above document as content.php, upload all three files to your server, and test it by going to index.php. You should first see your welcome message

Now, if you change the url to index.php?go=prices This will then display your pricing information and so on.

Note: While you can include html within your maincontent tag, in the document above, you must prefix all quotes ' with a backslash This will ensure that all html displays properly.

That's all there is to itenjoy!

Politica de confidentialitate | Termeni si conditii de utilizare



Vizualizari: 1097
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved