2010-11-29 29 views
1

我正在編寫本書「Zend Framework - 初學者指南」。 第三章的一部分描述了使用masterlayout進行工作。如何檢索Zend佈局中的參數

對於我的導航,我想動態設置身體的id-attrib。我怎樣才能從任何控制器獲取參數到這個佈局文件?

主佈局中的application.ini設置:

resources.layout.layoutPath = APPLICATION_PATH "/layouts" 
resources.layout.layout = master 

問候 弗蘭克

回答

2

您可以使用視圖瓦爾你需要傳遞到佈局腳本簡單變量:

在您的控制器中:

function indexAction() 
{ 
    $this->view->pageTitle = "Zend Layout Example"; 
} 

在佈局腳本:

<html> 
<head> 
    <title><?php echo $this->escape($this->pageTitle); ?></title> 
</head> 
<body></body> 
</html> 
2

要做到這一點,最好的辦法是使用佔位符。下面是一個例子佈局:

master.phtml 
------------ 
<html> 
    <head> 
     <title>My Master Layout</title> 
    </head> 
    <body id="<?= $this->placeholder('my_dynamic_id_attrib'); ?>"> 
    ... 
    </body> 
</html> 

注意,對於「id」屬性值與「<?=」開始。這與「<?php echo」相同,如果您使用Zend建議的默認.htaccess文件,它應該可以正常工作。如果「<?=」並不爲你工作,簡單地將其替換爲:現在

<body id="<?php echo $this->placeholder('my_dynamic_id_attrib'); ?>"> 

,在你的控制器,你可以通過設置您的動態ID:

IndexController.php 
------------------- 
public function indexAction(){ 

    //------------------------------------ 
    // Can either be $_GET or $_POST, etc. 
    $dynamicParam = $this->_getParam('id'); 

    //------------------------------------ 
    // Set the dynamic id 
    $this->view->placeholder('my_dynamic_id_attrib')->set($dynamicParam); 
} 
+0

THX,我用wajiws例如 – 2010-11-29 18:57:04