Template Class (osC_Template)
The template class (osC_Template) builds each page with the default or selected template design and controls where the elements of a page are to be shown, e.g. the header, footer, main content, side boxes, and content modules.
Content Classes and Content Page Files
Each page is accessed through a group based on the page requested where a content class is loaded that inspects the environment and loads a defined content page file. A content class code name can be passed as a parameter in the URL address to override the default content class from loading. The structure of a URL page request is defined as:
The group file defines a default content class to load if none is passed as a parameter in the URL address.
Content classes are responsible for inspecting the environment to load a defined content page file and are located in the following directory:
The online store index page is requested as:
This loads the default content class defined in the index group as no content class code name was passed as a parameter. The index.php file contains the following code to setup and load the defined content module:
<?php
$osC_Template = osC_Template::setup('index');
?>
This loads the index content class from the following directory:
If a content class code name was passed as a parameter in the URL address it would be loaded within the osC_Template::setup() class method rather than the default content class.
For example:
This will load the manufacturers content class from the index group located at:
The primary function of a content class is to inspect the environment to load a defined content page file. Content page files are located in the following directory:
A simple content class for the online store index page is:
<?php
class osC_Index_Index extends osC_Template {
var $_module = 'index',
$_group = 'index',
$_page_title,
$_page_contents = 'index.php',
$_page_image = 'table_background_default.gif';
function osC_Index_Index() {
global $osC_Language;
$this->_page_title = sprintf($osC_Language->get('index_heading'), STORE_NAME);
if ( isset($_GET['list']) && ( $_GET['list'] == 'column' ) ) {
$this->_page_title = $osC_Language->get('index_column_heading');
$this->_page_contents = 'index_column.php';
}
}
}
?>
The $_page_contents class variable defines a default content page to load and can be overwritten within the class constructor. By default this would load the following page content file:
Content classes are given the following class name:
The first letters of the group and content class code names are capitalized. The manufacturers content class described in a previous example would have the following class name:
Page Properties
Page Titles
The title of the page is defined in the content class $_page_title variable and can be retrieved with the getPageTitle() class method:
<title><?php echo $osC_Template->getPageTitle(); ?></title>
The existence of the page title can be checked and be externally overwritten with the hasPageTitle() and setPageTitle() class methods:
<?php
if ( !$osC_Template->hasPageTitle() ) {
$osC_Template->setPageTitle('New Page Title');
}
?>
Page Tags (Meta Elements)
Meta elements can be associated with a page to influence search engine rankings and are placed within the <head> section in the HTML source of the page with the getPageTags() class method:
<html>
<head>
<?php
if ( $osC_Template->hasPageTags() ) {
echo $osC_Template->getPageTags();
}
?>
</head>
</html>
Tags can be added to a page with the addPageTags() class method:
<?php
$osC_Template->addPageTags('keywords', 'apples');
?>
The first parameter is the meta group to add the meta elements to and the second parameter contains the meta element value. If multiple calls to addPageTags() are made using the same meta group, the multiple meta element values will be separated by commas when displaying the page tags.
Javascript
Javascript can be added to a page in three ways and are placed within the <head> section in the HTML source of the page. The hasJavascript() class method can check if Javascript is to be added to the page and is added with the getJavascript() class method:
<html>
<head>
<?php
if ( $osC_Template->hasJavascript() ) {
$osC_Template->getJavascript();
}
?>
</head>
</html>
The three methods available to add Javascript to a page are described as follows.
Javascript Blocks
Blocks of Javascript can be added with the addJavascriptBlock() class method:
<?php
$javascript = '<script language="javascript">alert("Hello!");</script>';
$osC_Template->addJavascriptBlock($javascript);
?>
External Javascript Files
External Javascript files can be referenced to with the addJavascriptFilename() class method:
<?php
$osC_Template->addJavascriptFilename('/httpdocs/oscommerce/ext/js/script.js');
?>
Externally Built PHP Javascript
Javascript that is built with PHP in an external file is included in the page with the addJavascriptPhpFilename() class method:
<?php
$osC_Template->addJavascriptPhpFilename('/httpdocs/oscommerce/ext/js/script.php');
?>