I looked at smarty once, my eyes have never been the same. Horribly bloated class. If I need to use a templating engine for a personal site I use Nathan Codding of the phpbb group's class. Anyone familiar with hacking phpbb will take to it in no time, imho its an excellent piece of code.
Most people just want a simple way of keeping html seperate from php (a good coding practice, so I dont agree with you php~pro, templating can be and is good) through variable replacement, ie placing {SOME_VAR} in their html and have it parsed and replaced with the data assigned to SOME_VAR.
With that in mind I'll post a little template class that does just that and an example php and html page.
save this as template.php
Code:
<?php
class template
{
var $template;
function template($tpl_file)
{
if (file_exists($tpl_file))
{
$this->template = join('', file($tpl_file));
}
else
{
die("Template file $tpl_file not found.");
}
}
function replace_vars($vars = array())
{
if (sizeof($vars) > 0)
{
foreach ($vars as $var => $data) {
$this->template = eregi_replace('{' . $var . '}', $data, $this->template);
}
}
else
{
die('No variables designated for replacement.');
}
}
function output()
{
echo($this->template);
}
function destroy()
{
$this->template = array();
}
}
?>
save as template_test.html
Code:
<html>
<head>
<title>Template Example</title>
</head>
<body>
You should see the words Testing Template Var 1 on the next line.
{template_test_1}
You should see the words Testing Template Var 2 on the next line.
{template_test_2}
</body>
</html>
save as index.php
Code:
<?php
require_once('template.php');
$template_variables = array();
$template_variables["$template_test_1"] = "Testing Template Var 1";
$template_variables["$template_test_2"] = "Testing Template Var 2";
/* Alternatively you can assign variables when you create the array.
$template_variables = array(
"template_test_1" => "Testing Template Var 1",
"template_test_2" => "Testing Template Var 2"
);
*/
// Create a template class to hold our html content
$content = new template('template_test.html');
// Replace any {variables} in our html content
$content->replace_vars($template_variables);
// Output our content
$content->output();
// Destroy the template class, ready for a new one.
$content->destroy();
?>
Hopefully its easy enough for php beginners to understand.
Hack it slap it, do what you want with it. Hope it saves someone some time plodding through smarty when all they want is to seperate html from php.