is less more or is more more?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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:

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');
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>
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
Last edited by Luke on Mon Apr 23, 2007 10:15 am, edited 1 time in total.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I'm starting to get into OOP now and dont really quite understand how its done. Thank you for posting that though, that definitely gives me a start.

Wayne
User avatar
mpeacock
Forum Newbie
Posts: 10
Joined: Thu Apr 12, 2007 9:07 am
Location: Mobile AL

Re: is less more or is more more?

Post by mpeacock »

snowrhythm wrote:these might sound like a total n00b questions, but regardless, i'm going to ask them. :D

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?
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).

Low Coupling/High Cohesion is sometimes referred to as a General Responsibility Assignment Software Pattern (or GRASP Patterns). Google that for more info.

Cheers
Post Reply