RE: Template System

Brenden <[email protected]> 31 Jul 2003 03:47:37 -0000
Newsgroups gmane.comp.web.oscommerce.devel
Message-ID <a1791d989042af5b2206c93e1a316e8e@osCommerce-Forums>
This message was sent from: Development
http://forums.oscommerce.com/viewtopic.php?p=204853#204853
----------------------------------------------------------------

Ill add a real life example:

You start out with a normal HTML page:
[code]<html>
<head>
	<title>Untitled</title>
</head>

<body>
<div id="header">This Is My Web Site</div>
<div id="columnLeft">This is my navigation</div>
<div id="content">This is my content</div>
<div id="footer">This is my footer</div>
</body>
</html>[/code]

On a normal web site you will have say 20 pages that look the same as this except the content is different on all pages.  If we want to change how our header looks we have to edit all 20 pages.  But if we implement a template structure we only have to edit 1 page to change our header.

Template Structure Example:
[code]<?php require(top.php); ?>
<div id="content">This is my content</div>
<?php require(bottom.php); ?>[/code]
top.php would look like:
[code]<html>
<head>
	<title>Untitled</title>
</head>

<body>
<div id="header">This Is My Web Site</div>
<div id="columnLeft">This is my navigation</div>[/code]
bottom.php would look like:
[code]<div id="footer">This is my footer</div>
</body>
</html>[/code]

So now we only need to change 1 file to have a different header on our entire site.

Now lets go back to our original page which looked like this:
[code]<html>
<head>
	<title>Untitled</title>
</head>

<body>
<div id="header">This Is My Web Site</div>
<div id="columnLeft">This is my navigation</div>
<div id="content">This is my content</div>
<div id="footer">This is my footer</div>
</body>
</html>[/code]

We want to add a template engine to it.  Using Smarty we would have this page:
[code]include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content
$smarty->assign('title', 'Untitled');
$smarty->assign('header', 'This Is My Web Site');
$smarty->assign('columnLeft', 'This is my navigation');
$smarty->assign('content', 'This is my content');
$smarty->assign('footer', 'This is my footer);

// display it
$smarty->display('index.tpl');[/code]

index.tpl would look like this:
[code]<html>
<head>
	<title>{ $title}</title>
</head>

<body>
<div id="header">{ $header}</div>
<div id="columnLeft">{ $columnLeft}</div>
<div id="content">{ $content}</div>
<div id="footer">{ $footer}</div>
</body>
</html>[/code]

Both the template engine and template structure examples are very basic but should provide some insight into the differences.

You can use a template engine and a template structure at the same time.