duande1146 2013-04-04 06:03
浏览 55
已采纳

PHP模板模式UI和内容解耦

With respect,

I am looking for a way to organize my HTML markup within PHP for use in a website which delivers dominantly static content(read: for a .edu website). (While this is the main thrust for this question, I am definitely interested in the non-static case as well since I may at some point soon "need" to deal with sessions and user login events etc)

The pattern I have often seen used for PHP (and other languages like Coldfusion, .NET, etc) is as follows:

// define header stub in header.php
<!DOCTYPE html>
<html>
    <body>
        <div id="headerWrapper">
            <!-- header content goes here -->
        </div>
        <div id="contentWrapper">
// end header stub

// define footer stub in footer.php
        </div><!-- end contentWrapper -->
        <div id="footer">
            <!-- footer content goes here -->
        </div>
    </body>
</html>
// end footer stub

To use this pattern, I would define a content file thusly:

// define content stub in contentFile.php
<?php include("header.php") ?>
            <p>hello world</p>
            <!-- other content goes here -->
<?php include("footer.php") ?>
// end content stub

I am looking for more. The problem I have with the above example is the level of coupling required by the general pattern. That is, there are some significant dependencies between header.php and footer.php for example (namely page structure, the dangling contentWrapper div tags, and, on a lower level, the inevitable css and js includes that are not required by every page). I am looking specifically for encapsulation. My ideal content page would look like this:

    // ideal.php
    <?php
        $someObj.pageTitle="My First Page!!!";
        $someObj.headInclude("/css/960.css");
        $someObj.headInclude("/js/myFancyThing.js");
        $someObj.preferedTemplate = "default"; /* or maybe "MathDept" or something like that */
    ?>
    <!-- page content goes here, just semantic and structural markup, let the 
template handle H1 and Paragraph formatting :) -->
    // end ideal stub

This last pattern is something I would very much like to see. It seems to me a much more elegant solution for the decoupling of content from presentation. I don't know how to articulate this in PHP but I suspect it is feasible. Any help would be most appreciated! Thank you!!! :)

/* I hope what I have asked is appropriately scoped and titled. Please forgive me otherwise; I am still a bit green with Stack Exchange, PHP and pretty much everything lol */

  • 写回答

1条回答 默认 最新

  • dtsps00544 2013-04-04 12:06
    关注

    With this kind of (excellent) thinking and approach you'll very quickly find yourself reinventing the MVC design pattern. I would strongly suggest you look at the Zend Framework's approach to the presentation layer. They use a two step design (layout + view). Here's a simplistic example of the pattern:

    Controller:

    public function someAction()
    {
        // Add the CSS files.
        $this->view->headLink()
            ->appendStylesheet($this->view->baseUrl() . '/css/site.css')
            ->appendStylesheet($this->view->baseUrl() . '/css/ie6.css', 'screen', 'IE 6')
            ->appendStylesheet($this->view->baseUrl() . '/js/jquery/plugins/ui/css/ui-lightness/jquery-ui-1.8.18.custom.css', 'screen')
    
        // Add the JavaScript files.
        $this->view->headScript()
            ->appendFile($this->view->baseUrl() . '/js/jquery/jquery-1.9.1.min.js')
            ->appendFile($this->view->baseUrl() . '/js/jquery/plugins/ui/jquery-ui-1.8.17.custom.min.js')
            ->appendFile($this->view->baseUrl() . '/js/modernizr.js');
    
        // Add the meta tags.
        $this->view->headMeta()
            ->appendHttpEquiv('X-UA-Compatible', 'IE=edge')
            ->appendName('description', '...')
            ->appendName('keywords', '...');
    
        // Render the View.
        $this->view->render();
    }
    

    Layout:

    <?php
    $this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
    echo $this->doctype(); ?>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <?php echo $this->headMeta(); ?>
        <?php echo $this->headTitle(); ?>
        <?php echo $this->headLink();?>
        <?php echo $this->headScript();?>
    </head>
    <body>
    
    
        <div id="wrapper">
    
            <?php echo $this->partial('partials/header.phtml');?>
    
            <?php echo $this->layout()->content;?>
    
            <?php echo $this->partial('partials/footer.phtml');?>
    
        </div>
    
    </body>
    </html>
    

    View:

    <h1>This View script is rendered by the View and will get injected into the layout.</h1>
    <div>Page specific markup goes in this file</div>
    

    If you do decide to go down this route, think hard before writting the framework code yourself.

    Link: http://framework.zend.com/manual/2.0/en/modules/zend.view.quick-start.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看