Knightnet Site Design - Template Examples, how to use the templates

You are in section: Home > Site Design

Example 1 - Short example with "manual" content

It doesn't get any simpler than this! The content here is "manual" because you leave PHP and simply write some HTML, the template wraps the important structures around this. Most pages on the Knightnet web site are like this. The output produced includes the standard headings, footings, breadcrumb and left navigation menus as shown on this page.

<?php
require_once('template.class.inc');
$Template=new Template();
$Template->outputTop();

<
p>My HTML here</p>

<?
php $Template->outputBottom(); ?>

Example 2 - Short example using the content variable

Also very simple. The content here is assigned to a standard variable for output. This is most useful where you want PHP to do something with the string.

<?php
require_once('template.class.inc');
$Template=new Template();
$Template->content=<<<ZZZ
<p>My HTML here</p>
<p>Don't forget that if you want to include text like &lt; or \$ it has to be escaped or included
as HTML entities.</p>
ZZZ;
# Example of PHP output:
$Template->content.=phpinfo();
$Template->output(); ?>

Example 3 - The shortest example (content provided by external file)

OK, I lied in example 1 as this is simpler still! Here the content comes from an external file. You can override the default file name using "$Template->setVar('incFile',array('file1.inc','file2.inc'))" so you can see that several files can be included.

<?php
# If the file containing this code is called "index.php" then the content
# will be in index.inc be default.
require_once('template.class.inc');
$Template=new Template();
$Template->output(); ?>

Combining content

These three methods of providing content can even be combined. The order of output is:

  • $Template->content variable
  • "Manual" content
  • Included file/s

Example 4 - Search engine friendly

OK, you've seen the really simple stuff. That is useful as it is - but it gets even better.

The template is designed to automatically help search engine placement. Simply include a title and, optionally, a description. The title is repeated on the page as an <h1>. All you need to do is then make sure that the content is relevant to the title.

<?php
require_once('template.class.inc');
$Template=new Template();
$Template->setVar('title','Knightnet Site Design - Template Examples, how to use the templates');
$Template->setVar('metaDescription','Examples of how to use the Knightnet templates');

$Template->output(); ?>

In addition, the automenu to the left is actually AFTER the main content which helps with text only or otherwise limited browsers (e.g. hand-held computers) and ensures that the content gets the weighting it deserves when the search robots index it.

These are very simple search engine optimisations but they really do work. What's more, they work without compromising the content or the human friendly layout of the page.

Sections:

Pages:

Valid HTML 4.01 iconValid CSS icon
© Copyright Julian Knight, July 2008 All rights reserved.
Page: Updated 2008-07-10 08:50:07, Author Julian Knight, Version 2.0