Advanced Template System
Posted: Tue Jun 29, 2004 3:17 pm
I'm looking for a more advanced template system, where I can use if/elseif/else (and possibly foreach) functions inside a template.
Nicknamed "HTML Logic" or "Logic HTML", it looks something like this:
If the above code could be better modified, post please.
Now, onto the "simple" template system that I have now.
Template Class:
It adds the variables from an array, loads the file, and replaces the given variables with their value.
Next, the actual template file:
Now, to run it:
Output:
It works exactly how it should, but as stated earlier, it's very simple. Have any of you made, or know of, an advanced template engine (that I can use in commercialware, and preferably not Smarty)? I'm trying to keep it very fit, clean and run extremely fast.
All I need is the base idea of how you would do it, and any example code if available.
Thanks,
Don
Nicknamed "HTML Logic" or "Logic HTML", it looks something like this:
Code: Select all
{if variable == "12"}
hi, im twelve!
{/if}
{else if variable == "16"}
hi, im sixteen!
{/else if}
{else}
hi, im {variable}
{/else}Now, onto the "simple" template system that I have now.
Template Class:
Code: Select all
// Template Engine
class Template
{
// Define class variables
var $usable_vars = array();
var $_TEMP = array();
var $template_file = '';
var $template_handle = '';
var $template_content = '';
var $cache_path = '';
// function: Start template engine
function template()
{
global $_CACHE;
global $_USER;
$this->cache_path = $_CACHEї'setting']ї'docroot'] . $_CACHEї'setting']ї'template_dir'] . $_USERї'template'] . "/";
}
// function: Write variables to cache
function add_variables($variables = array())
{
if(count($variables) > 0)
{
foreach ($variables as $key => $value)
{
$this->useable_varsї$key] = $value;
}
}
}
// function: Open template file
function parse_template($template_file)
{
global $_CACHE;
$this->template_handle = @fopen ($this->cache_path . $template_file, r);
if ($this->template_handle)
{
$this->template_content = @fread ($this->template_handle, filesize ($this->cache_path . $template_file));
@fclose ($this->template_handle);
if(count($this->useable_vars) > 0)
{
foreach ($this->useable_vars as $key => $value)
{
$this->template_content = eregi_replace($_CACHEї'setting']ї'reghandler_front'] . $key . $_CACHEї'setting']ї'reghandler_back'], "$value", $this->template_content);
}
}
print $this->template_content;
}
else
{
print "Cannot find specified template file ('" . $this->cache_path . $template_file . "').<br />\n";
}
}
}Next, the actual template file:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>{site_title}</title>
<script type="text/javascript" src="./scripts/global.js"></script>
<link rel="stylesheet" href="./templates/{template_folder}/styles.css" type="text/css">
</head>
<body>
<center>Code: Select all
// Template: site_header
$template = new Template;
$template->add_variables(array( 'site_title' => $_CACHE['setting']['site_title'],
'template_folder' => $_USER['template']));
$template->parse_template("site_header.tpl");Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>BlastMB Dev Board</title>
<script type="text/javascript" src="./scripts/global.js"></script>
<link rel="stylesheet" href="./templates/default/styles.css" type="text/css">
</head>
<body>
<center>All I need is the base idea of how you would do it, and any example code if available.
Thanks,
Don