1

我不确定我是否可以在这里问一个更实际的问题,因为这个问题不像提供好的建议那样得到支持。

我有一个带有简单布局管理器(没有更好的术语)的前端控制器,它有效地加载默认(或自定义,如果如此指定)html“布局”或骨架,并根据用户页面请求,将插入另一个文件(称为视图文件)到骨架内容区域。

骨架可能看起来像:

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

虽然很简单,但这使得以后的更新变得容易,因为如果客户现在希望我添加一个 jquery 滚动条,我可以将它添加到单个布局中,因为不必编辑多个文档来实现相同的目标。

然而,我的朋友说他认为这不是一个好的解决方案,并认为我不应该拆分我的“视图”文件和布局,而是坚持使用前端控制器,我的 html 页面应该使用 php include(包括页眉和页脚)和将我的 html 页面保存为 1 个文件,因为:

  1. 布局管理器会使我的应用程序不那么灵活,因为我将自己绑定到单个布局(除非我创建一个新布局并在我的页面控制器文件中指定它应该使用不同的布局)。
  2. 其他开发人员将很难理解逻辑,而我正在创造比需要更多的工作
  3. 简单的 HTML 开发人员将无法进行适当的测试,因为他们只能真正处理与整个 HTML 文档相对应的片段并看到更大的图景。
  4. 这更令人困惑,因为要对页面进行更改,我需要在视图文件或骨架文件中进行修改,并且您需要知道何时编辑骨架文件。

我将很快开始开发一个具有多个开发阶段的项目,对此我做出决定至关重要,因为我希望应用程序尽可能灵活并创建尽可能少的工作,但也要构建它这样我就可以轻松地对网站进行修改、改进或更改。我真的很重视一些建议,因为这个论点现在是基于 2 个人的意见。任何需要考虑的要点,以及可能有关更大的主流框架和 CMS 工作的信息也将帮助我做出更好的决定。

4

4 回答 4

3

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.

于 2011-11-19T10:48:18.100 回答
1

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>
于 2011-11-19T11:20:25.333 回答
0

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

于 2014-03-04T08:42:56.377 回答
-1

看看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

于 2011-11-19T10:39:20.240 回答