zendframework:Zend Framework 入门(4)——页面布局

  Zend Framework 页面布局模块——Zend_Layout——既可以跟 MVC 起使用也可以单独使用本文只讨论和 MVC 起使用情况

  1. 布局脚本

  在 application/views 下创建个 layouts 文件夹主布局脚本 layout.phtml 代码如下:

  <?php echo $this->doctype('XHTML1_STRICT') ?>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; char=utf-8" />
  <?php echo $this->headTitle ?>
  <?php
  $this->headLink->appendStylesheet("/styles/.css");
  // add more links ...
  ?>
  <?php echo $this->headLink ?>
  </head>
  <body>
  <div id="header">
  <?php echo $this->partial('header.phtml') ?>
  </div>
  <table>
  <tr>
  <td valign=top>
  <div id="leftcolumn">
  <?php echo $this->partial('leftcolumn.phtml') ?>
  </div>
  </td>
  <td valign=top>
  <div id="content">
  <?php echo $this->layout->content ?>
  </div>
  </td>
  </tr>
  </table>
  <div id="footer">
  <?php echo $this->partial('footer.phtml') ?>
  </div>
  </body>
  </html>


  除了 layout.phtml 的外还需要编写 header.phtmlleftcolumn.phtmlfooter.phtml以及 .css 等文件

  Zend Framework 文档中用个视图表示了页面布局应用



  2. 设置页面布局

  在 MVC 下设置页面布局非常简单编辑 html/index.php加入下面两行代码:

  /** Setup layout */
  require_once 'Zend/Layout.php';
  Zend_Layout::startMvc($rootPath . '/application/views/layouts');


  注意:在启动页面布局后要调整已有各个页面把不需要 html 元素如<header> <title> <body> 等去掉另外可以通过 $this->headTitle 来设置页面题头

  改变页面布局也很简单只需在控制器中用下面代码即可:

  $this->_helper->layout->Layout('_layout');

  如果个控制器所有动作都使用同个页面布局可以通过控制器来设置:

  public function init {
  parent::init;
  $this->_helper->layout->Layout('_layout');
  }


Tags:  zendframework安装 zendframework教程 zendframework代码 zendframework

延伸阅读

最新评论

发表评论