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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示