EDIT: ok never mind i figured out my previous question but am confused as to what the object Template should look like. I created these files and when i tried to run them in my browser php threw a fatal error saying that "tmplt was not defined. I'm not sure what the template object should look like.
Thanks,
Wayne
is less more or is more more?
Moderator: General Moderators
well you could use any number of template engines (smarty, for example). If you wanted to just create a really simple template class, something like this would work.
Not tested:
templates/profile.tpl.php
Like I said, this is very simple. I wouldn't use it. I'd use one of the libraries out there (what I use is Zend Framework's View class which I extend). Another one is template_lite
Not tested:
Code: Select all
class Template
{
var $_vars = array();
function assign($key, $val)
{
$this->_vars[$key] = $val;
}
function get($key)
{
return isset($this->has[$key]) ? $this->_vars[$key] : null;
}
function has($key)
{
return isset($this->_vars[$key]);
}
function escape($string)
{
return htmlentities($string);
}
function render($file)
{
if (file_exists($file))
{
ob_start();
include $file;
return ob_get_clean();
}
else
{
// handle your error however you like
}
}
}
$template = new Template;
$template->assign('username', 'The Ninja Space Goat');
$template->assign('favband', 'Tool');
$template->assign('middlename', 'Daniel');
$template->assign('amountofteeth', 4);
echo $template->render('templates/profile.tpl.php');Code: Select all
<html>
<head>
<title><?php echo $this->escape($this->get('username')); ?></title>
</head>
<body>
Username: <?php echo $this->escape($this->get('username')); ?>
Favorite band: <?php echo $this->escape($this->get('favband')); ?>
Middle name: <?php echo $this->escape($this->get('middlename')); ?>
Amount of teeth: <?php echo $this->escape($this->get('amountofteeth')); ?>
</body>
</html>
Last edited by Luke on Mon Apr 23, 2007 10:15 am, edited 1 time in total.
Re: is less more or is more more?
This is actually a pretty deep question that gets to the issues of cohesion and coupling in OOP. In general, you should opt for high cohesion and low coupling, thought it's not always easy to do this in practice (since you might be extending or adapting someone else's code).snowrhythm wrote:these might sound like a total n00b questions, but regardless, i'm going to ask them.
in my applications is it better to have several, one-task functions, or a few long, multipurpose functions?
which is better programming practice? and is there a performance boost or hit for either one?
Low Coupling/High Cohesion is sometimes referred to as a General Responsibility Assignment Software Pattern (or GRASP Patterns). Google that for more info.
Cheers