Frage

I am not sure if I am allowed to ask a more practical question here as this question is not as much support as just giving good advice.

I have a front controller with a simple layout manager (lack of a better term) which effectively loads a default (or custom if so specified) html "layout", or skeleton, and based on the users page request, will insert another file (called view files) into the skeletons content area.

A skeleton could look something like:

<div id="header">
    <h1>head goes here</h1>
<div id="content">
    <?php $this->insertContent(); ?>
</div>
<div id="footer">
    <p>footer goes here</p>
</div>

Although pretty simple, this makes it easy to make future updates because if a client would now want me to add a jquery scroller, I can add it to the single layout as appose to having to edit multiple documents to achieve the same thing.

My friend however says that he thinks this is not a good solution and believes that I should not split my "view" files and layouts, but rather stick with the front controller and my html pages should use php include (include header and footer) and keep my html pages in as 1 file because:

  1. A layout manager would make my application less flexible as I am binding myself to a single layout (unless I create a new layout and in my page controller file specify that it should use a different layout).
  2. Other developers would have a tough time understanding the logic and I am creating more work than is needed
  3. Simple HTML developers would not be able to do proper testing as they can only really work on snippets as appose to whole HTML documents and see the bigger picture.
  4. It's more confusing because to make changes to a page I need to make ammendments in either a view file or skeleton file and you need to know when skeleton file to edit.

I will soon start to develop a project which will have multiple development stages, and it is crucial for me to make a decision about this as I want the application to be as flexible as possible and create as little work as possible, but also build it so that I can easily make ammendments, improvements or changes to the site. I would really value some advice as this argument now is based on 2 people's individual opinions. Any points to consider, and possibly information as to how bigger mainstream frameworks and CMS's work would also help me make a better decision.

War es hilfreich?

Lösung

What you do is a so called Two Step View Pattern.

It is a common pattern and if you document it as such, it's easy for any developer to understand it.

Every pattern has it's pro and cons, I suggest to take a read of the according chapter in the book by Fowler to get a good introduction and a base you can make your thoughts of.

The term Layout is more broad but quite well understood by developers as well, but not that precise.

Andere Tipps

Zend_Layout is the only thing you'll ever need for layout management, very helpful.

Though you can implement your own one very easily. Something like this:

<?php

class Layout
{

    private $_data = array();

    public function __set($name, $value)
    {
        $this->_data[$name] = $value;
    }

    public function __get($name)
    {
        return array_key_exists($name, $this->_data) ? $this->_data[$name] : NULL;
    }

    public function __isset($name)
    {
        return isset($this->_data[$name]);
    }

    public function __unset($name)
    {
        unset($this->_data[$name]);
    }

}

// Usage

$layout = new Layout();

$layout->title = 'Welcome to my application mate!';

$layout->content = load_content_from_template('template_file.php');

$layout->scripts = array(
    'jquery',
    'jquery.plugin'
);

$layout->scripts[] = 'common';

?>

// Inside the layout file, assuming that $layout variable is available in this scope.

<html>
<head>
    <title><php echo $layout->title ?></title>
</head>
<body>

    <div id="content">
        <php echo $layout->content ?>
    </div>

    <?php
        foreach ($layout->scripts AS $script)
            echo '<script type="text/javascript" src="/path/to/scripts/directory/' . $script . '.js"></script>';
    ?>

</body>
</html>

Try using https://github.com/mahadazad/php-layout-manager its simple and can be integrated with codeigniter aswell.

have a look at Smarty.

http://www.smarty.net/

WordPress uses a variant of Smarty, as does my own CMS. I'm sure the other large CMSes use Smarty or an alternative which does the same thing

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top