facebook twitter hatena line google mixi email

Php/zend framework/レイアウトメモ

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

レイアウトを使う

application.iniに以下を設定

resources.layout.layoutpath = APPLICATION_PATH "/layouts"

application/Bootstrap.phpに以下を設定

protected function _initViewHelpers()
{
  $this->bootstrap('layout');
  $layout = $this->getResource('layout');
  $view = $layout->getView();
  $view->doctype('XHTML1_STRICT');
  $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
  $view->headTitle()->setSeparator(' - ');
  $view->headTitle('Zend Framework Tutorial');
}

application/layouts/layout.phtmlに以下を記述

<?php 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(); ?>
</head>
<body>
<div id="content">
    <h1><?php echo $this->escape($this->title); ?></h1>
    <?php echo $this->layout()->content; ?>
</div>
</body>
</html>

アクションに以下のように設定すると代入される

$this->view->title = "My Albums";
$this->view->headTitle($this->view->title, 'PREPEND');

smartyを使ったlayout

  • Bootstrap.php
   protected function _initLayout()
   {
       $options = array(
           'layout'=>'layout',
           'layoutPath'=> __DIR__ . '/views/layouts',
           'viewSuffix' => 'tpl',
       );
       $layout=Zend_Layout::startMvc($options);
   }
  • controllers/TestController.php
   public function postDispatch()
   {
       $this->_helper->viewRenderer->setNoRender();
       $this->view->content = $this->view->render(sprintf('%s/%s.tpl', $this->_request->controller, $this->_request->action));
   }
  • views/layouts/layout.tpl
<html>
<body>
{{$content}}
</body>
</html>