在使用ZF時,如果在layout要引入css時,常常會遇到路徑出錯,或者想要用同一個layout但卻要引入不同的css
要克服這個問題就要先在Controller端先處理
Zend_View也支援View Helper。View Helper能協助我們快速地對樣版上的元素做處理
以BaseUrl為例,BaseUrl可以幫助我們取得根目錄的路逕
首先要先在application/config/application.ini加入路徑
resources.frontController.baseUrl = "/public"
使用方法(輸出根目錄路逕)
<?php
//輸出根目錄路徑
echo $this->view->baseUrl();
//另外也可以直接在根目錄後面串上路徑
echo $this->view->baseUrl("/css/test.css");
?>
實際使用範例:
FooController
<?php
class FooController extends Zend_Controller_Action {
public function indexAction() {
$this->view->css = $this->view->baseUrl("/htdocs/css/test.css");
}
}
?>
layout.phtml
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>ZF TEST</title> <link type="text/css" rel="stylesheet" href="<?php echo $this->css; ?>" media="screen"> </head> <body> <div id="hd"><h1>Header</h1></div> <!-- $this->layout()->content就是把view script的所有輸出 --> <div id="bd"><?php echo $this->layout()->content; ?></div> <div id="ft"><small>Footer</small></div> </body> </html>