程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> zend framework 使用layout進行布局

zend framework 使用layout進行布局

編輯:PHP基礎知識
 

進入application\configs修改application.ini,添加的內容

;=========== layout布局,由view來配置
resources.layout.layout = "we use resources.view.params.module.layout instead"
resources.layout.layoutPath = "we use resources.view.params.module.layoutPath instead"
再添加一些內容,每一塊的第四和第五行

;=========== front和admin模塊的view參數,包括scripts文件路徑及前綴,layout路徑及名稱
resources.view.params.front.basePath = APPLICATION_PATH "/views/front/"
resources.view.params.front.helperPathPrefix = "Views_Helpers_Front_"
resources.view.params.front.helperPath = "Views/Helpers/Front/"
resources.view.params.front.layout = "frontlayout"
resources.view.params.front.layoutPath = APPLICATION_PATH "/views/front/layout/"

resources.view.params.admin.basePath = APPLICATION_PATH "/views/admin/"
resources.view.params.admin.helperPathPrefix = "Views_Helpers_Admin_"
resources.view.params.admin.helperPath = "Views/Helpers/Admin/"
resources.view.params.admin.layout = "adminlayout"
resources.view.params.admin.layoutPath = APPLICATION_PATH "/views/admin/layout/"
接著進入Zend\Controller\Plugin修改module.php文件,在routeShutdown函數後面添加以下代碼

// 加載layout並配置
$bootstrap->bootstrap('Layout');
$layout = $bootstrap->getResource('Layout');
$layout->setLayoutPath($moduleParams['layoutPath'])
->setLayout($moduleParams['layout']);
名在ini裡設置,前面已設置為frontlayout了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title><?php echo $this->pagetitle;?></title><style type="text/css">
body{margin:0; padding:0;}
h1,h2{margin:0; padding:0;}.head{width:1000px;height:100px;margin:0 auto;background-color:#9CC;}.main{width:1000px;margin:0 auto;overflow:hidden;}.sidebar{width:200px; height:500px;background-color:#99C;float:left;}.sidebar ul{margin:0;}.mainbox{float:left;padding-left:15px;}.foot{width:1000px;height:80px;margin:0 auto;background-color:#696;}</style></head><body>
<?php echo $this->layout()->head; ?>

<div class="main">
<?php echo $this->layout()->sidebar; ?>
<?php echo $this->layout()->content; ?>
</div>

<?php echo $this->layout()->foot; ?></body></html>
進入application\views\front新建文件夾layout,用來放layout文件,並新建frontlayout.phtml文件,
默認文件接著到application\views\front\scripts新建
head.phtml,foot.phtml,sidebar.phtml等內容視圖
head.phtml

<div class="head">
<h1><?php echo $this->head;?></h1>
</div>
sidebar.phtml

<div class="sidebar">
<h2><?php echo $this->sidebar;?></h2>
<ul>
<li>sidebar</li>
<li>sidebar</li>
<li>sidebar</li>
<li>sidebar</li>
<li>sidebar</li>
</ul>
</div>
foot.phtml

<div class="foot">
<h2><?php echo $this->foot;?></h2>
</div>
最後,進入application\control\front\controllers,修改控制器IndexController.php的代碼,在indexAction()裡寫入

$this->view->pagetitle = 'Zend Layout Example';
$this->view->head = 'This is head.';
$this->view->sidebar = 'This is sidebar.';
$this->view->main = 'This is main.!';
$this->view->foot = 'This is foot.';

//內部視圖加載
$response = $this->getResponse();
$response->insert('head', $this->view->render('head.phtml'));
$response->insert('sidebar', $this->view->render('sidebar.phtml'));
$response->insert('foot', $this->view->render('foot.phtml'));

//更換layout
//$this->_helper->layout->setLayout('frontlayout2');

//停用layout
//$this->_helper->layout->disableLayout();
結束

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved