麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

走進Zend Framework框架編程6(視圖)

2019-11-17 04:11:17
字體:
來源:轉載
供稿:網友

視圖

部分內容包括:視圖,模板,視圖幫助類等。

6.0視圖介紹
在Zendframework的MVC編程模型中,視圖(View)是在控制器的控制和指揮下,用來對程序邏輯進行呈現(Render)的。呈現的結果,就是我們在瀏覽器里看到的文字、圖片、表單等各種網頁元素及其字體、顏色、樣式等各種效果。
Zend_View Class就是負責視圖工作的類,它有效地完成了視圖與程序邏輯的分離。它提供了視圖幫助、輸出過濾和變量轉義等功能。
Zend_View還是一個模板系統,我們可以用php作為我們的模板語言。當然ZF還可以在View腳本里使用其他第三方的模板系統,比如PHPLib和Smarty等。
使用Zend_View時主要分兩步,首先聲明一個Zend_View實例,把變量等賦給它,然后使用控制腳本,根據視圖腳本呈現出結果。
例如:
控制腳本(在控制器文件的action里,例如IndexController.php中的函數dispdataAction()):……

Function dispdataAction()

{

……

$data = ‘to view’; //數據變量

Zend_Loader::loadClass(’Zend_View’);

$view = new Zend_View(); //實例化

$view->books = $data;  //賦值

echo $view->render(’view.php’);

}
視圖腳本轉義輸出語句(在視圖腳本文件里,本例是view.php):
<?

php echo $this->escape($this->data

?>
6.1引導文件中setParam(’noViewRenderer’, <bool>)語句的再解釋
Index文件中$fc->setParam(’noViewRenderer’, <bool>);語句和視圖的某性特性有很大關系,比如視圖存放的路徑等一些默認屬性都受該開關屬性的影響。
false 是noViewRenderer的默認值。也就是說,如果沒有該語句,則認為noViewRenderer參數被設置為false。
noViewRenderer參數被設置為false:意味著控制器使用ZF默認的視圖特性,比如視圖文件默認必須存放于views/script/<action>/文件夾下,視圖文件必須存在,而且其名字必須為<action>.phtml。.phtml是ZF使用的PHP腳本文件,和普通PHP文件沒有本質區別。同時,視圖對象默認被實例化為$view變量,在控制腳本中使用$view的形式為:$this->view->…。
noViewRenderer參數被設置為true:意味著控制器不使用ZF默認的視圖特性,而是通過顯式的實例化Zend_view對象,通過我們自己的代碼來設置視圖對象的屬性和方法。本部分“視圖介紹”中就是顯式聲明和使用視圖對象的例子。為了程序的靈活性和可控行,我們自然建議把noViewRenderer 設置為 true,這也是比較通常的做法。

6.2視圖對象的Options
視圖對象的Options選項進一步規定了視圖腳本呈現過程中的一些細節。這些選項可以通過在聲明視圖對象時指定,在構造函數里設置,也可以通過set……()方法來指定。
—basePath基本路徑
設置方法:setBasePath(), addBasePath()
例如目錄結構:base/path/

  helpers/

  filters/

  scripts/
用$view->setBasePath(”base/ path/”);語句設置基本路徑后,在沒有$view->setScriptPath(’……’);語句直接指定腳本路徑時,就會自動在base/path/scripts/下搜索視圖腳本文件,如果使用了視圖助手和過濾器,就會分別自動在helpers/和 filters/文件夾下搜索。
—encoding字符編碼
用來在使用htmlentities()、htmlspecialchars()或其他操作時,指定字符編碼。
設置方法:setEncoding()
默認編碼是ISO-8859-1 (latin1)。
—escape回調函數
用于在視圖呈現時調用該函數。后邊有示例。
設置方法:setEscape()
—filter過濾器
用于在視圖呈現后調用過濾方法。
設置方法:setFilter(), addFilter(),
—strictVars
當視圖視圖發送一個未初始化的變量時,用該選項指定ZF收購給出一個提示或警告信息:
Notice: Key “xxx” does not exist ……
設置方法:strictVars(true)

6.3視圖對象的一些屬性存取方法
—getVars() 得到所有賦予的變量
—clearVars()清除所有賦予的變量
—getScriptPath($script) 得到給定腳本的路徑
—getScriptPaths()得到所有腳本的路徑
—getHelperPath($helper)得到某個指定助手類的路徑
—getHelperPaths()得到所有助手的路徑
—getFilterPath($filter) 得到某個指定過濾器類的路徑
—getFilterPaths()得到所有過濾器的路徑

6.4視圖的路徑:

6.4.1視圖腳本的搜尋路徑
如果引導文件中$fc->setParam(’noViewRenderer’, false);
則默認指定視圖文件views/scripts/[controller_name]/[action].phtml
在實際的程序代碼中,為了獲得可定制的靈活性,都在控制器中實際指定了視圖文件的路徑:
指定路徑例句:$view->setScriptPath(’…/views’);或
$view->addScriptPath(’…/views’);
這個時候,引導文件必須有$fc->setParam(’noViewRenderer’, true);語句,即設置noViewRenderer為true。
6.4.2視圖腳本的搜尋的優先順序$view = new Zend_View();

$view->addtScriptPath(’…/views1′);

$view->addScriptPath(’…/views2′);

$view->addScriptPath(’…/views3′);
Zend的手冊是這樣說的:“如果沒有指定任何搜素路徑,則在控制器文件下搜索視圖文件。”但是通過實際環境測試,發現這時會報告錯誤:
“no view script directory set; unable to determine location for view script”
看起來最少需要指定一個搜索路徑。
如果指定了多條搜素路徑,則最后的搜索路徑優先。也就是說,如果所有的搜索路徑下有相同的視圖文件,則最后路徑下的起作用,它覆蓋了前邊路徑下的視圖文件。

6.5視圖控制腳本及其變量傳遞
ZF的控制器是實例化和設置Zend_View的地方。在這里,我們給視圖賦值并告訴它用指定的視圖腳本去呈現它們。
6.5.1給視圖對象賦值
示例:$view->Variable=”……”;的形式

function assign1Action()

{

  $view = new Zend_View();

  $view->setScriptPath(’views’);

  $view->strictVars(true);

  $view->a = “Hay”;

  $view->b = “Bee”;

  $view->c = “Sea”;

  $view->d;

  echo $view->render(’tp_abc.php’);

   }
示例:
$view-> assign(’ Variable ‘, “……”);的形式function assign2Action()

{

  $view = new Zend_View();

  $view->setScriptPath(’views’);

  $view->assign(’a', “Hay”);

  $view->assign(’b', “Bee”);

  $view->assign(’c', “Sea”);

  echo $view->render(’tp_abc.php’);

   }
示例:數組function assign3Action()

{

  $view = new Zend_View();

  $view->setScriptPath(’views’);

  $array = array(

‘a’ => “Hay”,

‘b’ => “Bee”,

‘c’ => “Sea”,

  );

  $view->assign($array);

  echo $view->render(’tp_abc.php’);

   }
示例:對象function assign4Action()

{

  $view = new Zend_View();

  $view->setScriptPath(’views’);

  $obj = new StdClass;

  $obj->a = “Hay”;

  $obj->b = “Bee”;

  $obj->c = “Sea”;

  $view->assign((array) $obj);

  echo $view->render(’tp_abc.php’);

   }
示例:
使用回調函數。function myhtmlentityAction()

{

  $view = new Zend_View();

  $view->setScriptPath(’views’);

//自定義類,在/models文件夾下的myclass.php文件中定義

  $mycls = new myClass();

//調用$mycls類的myHtmlEntity方法

  $view->setEscape(array($mycls, ‘myHtmlEntity’));

  $obj = new StdClass;

  $obj->a = “the Words hay bee sea “;

  $obj->b = “this is bee”;

  $obj->c = ” this is sea”;

  $view->assign((array) $obj);

  echo $view->render(’tp_abc.php’);

   }
tp_abc.php視圖腳本模板文件內容:
<?php

  echo ‘a = ‘ . $this->escape($this->a).’<br>’;

  echo ‘b = ‘ . $this->escape($this->b).’<br>’;

  echo ‘c = ‘ . $this->escape($this->c).’<br>’;

?>
myclass.php文件內容:
<?php

  class myClass

  {

public function __construct($options = null)

{

}

function myHtmlEntity($val)

{//把所有單詞首字母變為大寫

  return ucwords($val);

}

  }

?>
輸出結果:
a = The Words Hay Bee Sea
b = This Is Bee
c = This Is Sea
6.6視圖腳本的變量轉義輸出(escaping output)
視圖腳本得到變量以后,需要通過轉義進行輸出,變成頁面可以顯示的Html代碼。
輸出語句的格式:
echo $this->escape($this->variable);
$variable變量是在視圖腳本里用render方法傳遞過來的。
一般情況下,傳遞的變量是通過PHP的 htmlspecialchars()函數轉義的。而我們也可以實現我們自己的轉義函數。請參考以上“使用回調函數”示例。

6.7視圖腳本的模板系統—操作PHPLib類型的模板
模板系統進一步完美的實現了視圖與程序邏輯的分離。視圖腳本可以完美的操作PHPLib等類型的模板。

6.7.1PHPlib的安裝和調用
為了測試下面的示例,我們必須安裝PHPLib模板系統到我們的環境中。從網上下載到phplib-7.4.ZIP安裝壓縮包,解壓到安裝ZEND的library文件夾下,就完成了安裝。
為了在ZF的視圖腳本里調用得到模板類文件,必須在引導文件Index.php的set_include_path部分添加PHPLib模板類庫文件夾phplib-7.4/php到搜索路徑中。以下示例同時包含了Smarty模板引擎的類庫文件的搜索路徑:set_include_path(’.’ .

  PATH_SEPARATOR . ‘../library/’.

  PATH_SEPARATOR . ‘../library/phplib-7.4/php/’.

  PATH_SEPARATOR . ‘../library/Smarty-2.6.19/libs/’.

  PATH_SEPARATOR . ‘models/’.

  PATH_SEPARATOR . get_include_path()

);
注意,所有路徑都是以引導文件所在文件夾作為參照的。盡管視圖文件里所在文件夾不是引導文件所在根目錄,但在視圖文件里包含PHPLib類庫文件的語句include_once ‘template.inc’;仍然是以引導文件所在目錄作為參照的。

6.7.2在視圖文件里調用PHPLib模板
首先包含PHPLib類庫文件,然后聲明模板類的一個實例。使用模板類,首先需要指定一些屬性,比如指定模板所在路徑,指定模板文件等,然后用set_var傳遞模板變量,最后用parse方法調用模板文件。PHPLib模板系統的詳細用法請參考其幫助文檔。
示例:
<?php

  include_once ‘template.inc’;

  $tpl = new Template();

  $tpl->set_root(’views’);

  if ($this->books)

  {

$tpl->set_file(array(

“booklist” => “booklist.tpl”,

“eachbook” => “eachbook.tpl”,

));

foreach ($this->books as $key => $val)

{

  $tpl->set_var(’author’, $this->escape($val['author']));

  $tpl->set_var(’title’, $this->escape($val['title']));

  $tpl->parse(”books”, “eachbook”, true);

}

$tpl->pparse(”output”, “booklist”);

  }

  else

  {

$tpl->setFile(”nobooks”, “nobooks.tpl”);

$tpl->pparse(”output”, “nobooks”);

  }

?>
booklist.tpl文件內容:
<?php

  if ($this->books):

?>

  <table border=1>

  <tr>

<th>作者</th>

<th>書名</th>

  </tr>

  <?php

foreach ($this->books as $key => $val):

  ?>

  <tr>

<td><?php echo $this->escape($val['author']) ?></td>

<td><?php echo $this->escape($val['title']) ?></td>

  </tr>

  <?php endforeach; ?>

  </table>

<?php

  else:

?>

  <p>There are no books to display.</p>

<?php

  endif;
eachbook.tpl文件內容:<!– eachbook.tpl –>

<tr>

  <td>{author}</td>

  <td>{title}</td>

</tr>
6.8視圖腳本的模板系統—使用 Zend_View_Interface調用第三方模板引擎
我們還可以通過實現Zend_View_Interface接口,來得到一個可用的模板系統。
ZF對Zend_View_Interface接口的原始定義:/** Return the actual template engine object */
http://www.companysz.com/
public function getEngine();

/* Set the path to view scripts/templates */

public function setScriptPath($path);

/* Set a base path to all view resources */

public function setBasePath($path, $PRefix = ‘Zend_View’);

/* Add an additional base path to view resources */

public function addBasePath($path, $prefix = ‘Zend_View’);

/* Retrieve the current script paths */

public function getScriptPaths();

/* Overloading methods for assigning template variables as object properties */

public function __set($key, $value);

public function __get($key);

public function __isset($key);

public function __unset($key);

/* Manual assignment of template variables, or ability to assign multiple

  variables en masse.*/

public function assign($spec, $value = null);

/* Unset all assigned template variables */

public function clearVars();

/* Render the template named $name */

public function render($name);
使用該接口,我們可以很容易的把第三方的模板引擎,比如Smarty,包裝成Zend_View兼容的模板類。

6.8.1Smarty的安裝
下載Smarty軟件包,解壓到ZEND的library文件夾下,就完成了安裝。
為了在ZF的視圖腳本里調用得到模板類文件,必須在引導文件Index.php的set_include_path部分添加Smarty模板類庫文件夾Smarty-2.6.19/libs到搜索路徑中,參看前面PHPlib的安裝說明部分。
Smarty模板引擎需要建立template_dir和compile_dir文件夾才能工作。ZF手冊里的示例因為缺少這些設置而無法運行,正確的代碼片段如下:public function setScriptPath($path)

{

if (is_readable($path))

{

$this->_smarty->template_dir = $path;

$this->_smarty->compile_dir = $path;  //必須加語句:設置編譯路徑

$this->_smarty->cache_dir = $path;//設置緩存路徑

return;

 }
……
我們把對Zend_View_Interface接口的實現的類,放在models文件夾下的ZendViewSmarty.php文件中,該文件的內容如下:
<?php

require_once ‘Zend/View/Interface.php’;

require_once ‘Smarty.class.php’;

class ZendViewSmarty implements Zend_View_Interface

{

/**

 * Smarty object

 * @var Smarty

 */

protected $_smarty;

/**

 * Constructor

 * @param string $tmplPath

 * @param array $extraParams

 * @return void

 */

public function __construct($tmplPath = null, $extraParams = array())

{

$this->_smarty = new Smarty;

if (null !== $tmplPath) {

$this->setScriptPath($tmplPath);

}

foreach ($extraParams as $key => $value) {

$this->_smarty->$key = $value;

}

}

/**

 * Return the template engine object

 * @return Smarty

 */

public function getEngine()

{

return $this->_smarty;

}

/**

 * Set the path to the templates

 * @param string $path The directory to set as the path.

 * @return void

 */

public function setScriptPath($path)

{

if (is_readable($path)) {

$this->_smarty->template_dir = $path;

$this->_smarty->compile_dir = $path;

$this->_smarty->cache_dir = $path;

return;

}

throw new Exception(’Invalid path provided’);

}

/**

 * Retrieve the current template directory

 * @return string

 */

public function getScriptPaths()

{

return array($this->_smarty->template_dir);

}

/**

 * Alias for setScriptPath

 * @param string $path

 * @param string $prefix Unused

 * @return void

 */

public function setBasePath($path, $prefix = ‘Zend_View’)

{

return $this->setScriptPath($path);

}

/**

 * Alias for setScriptPath

 * @param string $path

 * @param string $prefix Unused

 * @return void

 */

public function addBasePath($path, $prefix = ‘Zend_View’)

{

return $this->setScriptPath($path);

}

/**

 * Assign a variable to the template

 * @param string $key The variable name.

 * @param mixed $val The variable value.

 * @return void

 */

public function __set($key, $val)

{

$this->_smarty->assign($key, $val);

}

/**

 * Retrieve an assigned variable

 * @param string $key The variable name.

 * @return mixed The variable value.

 */

public function __get($key)

{

return $this->_smarty->get_template_vars($key);

}

/**

 * Allows testing with empty() and isset() to work

 * @param string $key

 * @return boolean

 */

public function __isset($key)

{

return (null !== $this->_smarty->get_template_vars($key));

}

/**

 * Allows unset() on object properties to work

 * @param string $key

 * @return void

 */

public function __unset($key)

{

$this->_smarty->clear_assign($key);

}

/**

 * Assign variables to the template

 * Allows setting a specific key to the specified value, OR passing an array

 * of key => value pairs to set en masse.

 * @see __set()

 * @param string|array $spec The assignment strategy to use (key or array of key

 * => value pairs)

 * @param mixed $value (Optional) If assigning a named variable, use this

 * as the value.

 * @return void

 */

public function assign($spec, $value = null)

{

if (is_array($spec)) {

$this->_smarty->assign($spec);

return;

}

$this->_smarty->assign($spec, $value);

}

/**

 * Clear all assigned variables

 * Clears all variables assigned to Zend_View either via [email={@link]{@link[/email] assign()} or

 * property overloading ([email={@link]{@link[/email] __get()}/{@link __set()}).

 * @return void

 */

public function clearVars()

{

$this->_smarty->clear_all_assign();

}

/**

 * Processes a template and returns the output.

 * @param string $name The template to process.

 * @return string The output.

 */

public function render($name)

{

return $this->_smarty->fetch($name);

}

}

?>
控制腳本中對我們我的模板類的調用代碼:function smartyAction()

   {

  $view = new ZendViewSmarty(); //實例化新的模板類

  $view->setScriptPath(’views’); //設置模板文件路徑

  $view->book = ‘Enter Zend Framework Programme’; //傳遞變量給模板引擎

  $view->author = ‘張慶(網眼)’;

  echo $view->render(’bookinfo.tpl’); //視圖呈現

   }
我們看到,由于Smarty模板引擎的良好特性,除過實現上述接口的代碼比較復雜以外,我們這里的控制代碼要比應用PHPLib模板簡單得多。我們的數據不必像PHPLib引擎那樣,要把數據傳給視圖腳本,再由視圖腳本聲明PHPLib類,再把數據發送給模板去呈現。這里是在控制腳本里把數據直接傳遞給Smarty模板去呈現的。這是因為ZendViewSmarty實現的是Zend_View接口,它和Zend_View的用法是一樣的。

6.9視圖助手(Helper)
視圖腳本里經常有一些繁雜的事情,比如格式化日期、產生表單元素等等。這些可以用助手幫我們來完成。
助手類其實是一些以Zend_View_Helper_開頭的類,類名的最后一段是助手的名字,助手的名字必須是首字母大寫的,該類必須至少有一個以助手名字命名的方法。助手名通常是駝峰式命名,即它不會是大寫字母開頭的。類名是混合大小寫字格式。方法名也是駝峰式命名。
默認的助手的路徑通常指向Zend/View/Helper。即使用setHelperPath()方法重新指定了路徑,該路徑也會保持以使默認的助手能夠工作。

6.9.1ZF自帶的助手
示例代碼:
<?php

echo $this->form(’frm1′, array(’action’=>’action.php’, ‘method’=>’post’), false) .”/n”;

echo $this->formHidden(’id’, ’submited’);

$content = ‘Your Name:’ . $this->formText(’name’, ”, array(’size’ => 20)) .’<br>’;

$content .= ‘Password:’ . $this->formPassword(’pass’, ”, array(’size’ => 20));

echo $this->fieldset(’flst’, $content, array(’legend’=>’Name:’, ’style’=>’width:200pt’)) .’<br>’;

 

echo $this->formLabel(’email’, ‘Your Email:’);

echo $this->formText(’email’, [email=]‘[email protected]’[/email], array(’size’ => 32)) .’<br>’;

echo ‘Your Country:’;

echo $this->formSelect(’country’, ‘us’, null, $this->countries) .’<br>’;

echo ‘Would you like to opt in?’;

echo $this->formCheckbox(’opt_in’, ‘yes’, null, array(’yes’, ‘no’)) .’<br>’;

echo ‘Choose them:’;

echo $this->formMultiCheckbox(’chkbox’, ‘A’, null, array(’A'=>’valA’,'B’=>’valB’,'C’=>’valC’,'D’=>’valD’), ‘<br>’) .’<br>’;

echo ‘Choose one:’;

echo $this->formRadio(’radio’, ‘A’, null, array(’A'=>’valA’,'B’=>’valB’,'C’=>’valC’,'D’=>’valD’), ”) .’<br>’;

echo $this->htmlList($this->countries) .’<br>’;

echo $this->url(array(’k1′=>’v1′,’k2′=>’v2′,’k3′=>’v3′)) .’<br>’;

echo $this->formTextarea(’ta’, ”, array(’rows’=>’5′,’cols’=>’25′)) .’<br>’;

echo $this->formFile(’file’, ”, array()) .’<br>’;

echo $this->formButton(’btn’, ‘BUTTON’, array(’onClick’=>”));

echo $this->formSubmit(’OK’, ‘OK’);

echo $this->formReset(’reset’, ‘Reset’);

  ?>
6.9.2動作(Action)助手
允許我們在視圖腳本里執行某個控制器里的動作方法。
示例:<div id=”sidebar right”>

<div class=”item”>

  <?= $this->action(’assign1′, ‘Book’); ?>

</div>

  </div>

6.9.3區域(Partial)助手
區域助手的基本用法是在它自己的視圖范圍內解析另一個模板的片段,類似視圖腳本嵌套調用。
區域助手的基本用法:
示例:
booklist.php文件包含腳本片段:
<?php

  if ($this->books):

?>

  <table border=1>

  <tr>

<th>作者</th>

<th>書名</th>

  </tr>

  <?php

foreach ($this->books as $key => $val):

  ?>

  <tr>

<td><?php echo $this->escape($val['author']) ?></td>

<td><?php echo $this->escape($val['title']) ?></td>

  </tr>

  <?php endforeach; ?>

  </table>

<?php

  else:

?>

  <p>There are no books to display.</p>

<?php

  endif;

?>
在另一個視圖腳本通過Partial助手調用booklist.php文件:
<?= $this->partial(’booklist.php’, array(

  ‘books’ => array(

  array(’author’=>’zhangqing’,'title’=>’《book for php》’),

  array(’author’=>’zhangking’,'title’=>’《book for jsp》’),

  array(’author’=>’zhanghing’,'title’=>’《book for asp.net》’),

)));

?>

<?php echo str_repeat(’-', 60). ‘<br>’; ?>

<?php

  $model = array(

array(’key’ => ‘Mammal’, ‘value’ => ‘Camel’),

array(’key’ => ‘Bird’, ‘value’ => ‘Penguin’),

array(’key’ => ‘Reptile’, ‘value’ => ‘Asp’),

array(’key’ => ‘Fish’, ‘value’ => ‘Flounder’),

  );

?>

<dl>

  <?= $this->partialLoop(’partialLoop.phtml’, $model) ?>

</dl>

使用 PartialLoop 來解析可迭代的(Iterable)的模型
示例:
<?php

  $model = array(

array(’key’ => ‘Mammal’, ‘value’ => ‘Camel’),

array(’key’ => ‘Bird’, ‘value’ => ‘Penguin’),

array(’key’ => ‘Reptile’, ‘value’ => ‘Asp’),

array(’key’ => ‘Fish’, ‘value’ => ‘Flounder’),

  );

?>

<dl>

  <?= $this->partialLoop(’partialLoop.phtml’, $model) ?>

</dl>

以上代碼中partialLoop 調用了以下partialLoop.phtml的內容:

  <dt><?= $this->key ?></dt>

  <dd><?= $this->value ?></dd>

6.9.4占位(Placeholder)助手
示例:
<?= $this->doctype(’XHTML1_STRICT’) ?>

<?php

// setting meta keywords

$this->headMeta()->appendName(’keywords’, ‘framework php productivity’);

$this->headMeta()->appendHttpEquiv(’expires’, ‘Wed, 26 Feb 1997 08:21:57 GMT’)

 ->appendHttpEquiv(’pragma’, ‘no-cache’)

 ->appendHttpEquiv(’Cache-Control’, ‘no-cache’);

$this->headMeta()->appendHttpEquiv(’Content-Type’, ‘text/html; charset=UTF-8′)

 ->appendHttpEquiv(’Content-Language’, ‘en-US’);

$this->headMeta()->appendHttpEquiv(’Refresh’, ‘3;URL=http://www.some.org/some.html’);

echo $this->headMeta();

$this->headScript()->appendFile(’/js/prototype.js’)

   ->appendScript(’xx.js’);

// Putting scripts in order

// place at a particular offset to ensure loaded last

$this->headScript()->offsetSetScript(100, ‘/js/myfuncs.js’);

// use scriptaculous effects (append uses next index, 101)

$this->headScript()->appendScript(’/js/scriptaculous.js’);

// but always have base prototype script load first:

$this->headScript()->prependScript(’/js/prototype.js’);

echo $this->headScript();

// setting links in a view script:

$this->headLink()->appendStylesheet(’/styles/basic.CSS’)

 ->headLink(array(’rel’ => ‘favicon’, ‘href’ => ‘/img/favicon.ico’), ‘PREPEND’)

 ->prependStylesheet(’/styles/moz.css’, ’screen’, true);

// rendering the links:

echo $this->headLink();

?>

<?php

  $this->placeholder(’foo’)->setPrefix(”<ul>/n<li>”)

   ->setSeparator(”</li><li>/n”)

   ->setIndent(4)

   ->setPostfix(”</li></ul>/n”);

  $this->placeholder(’foo’)->set(”Some text for later-1″);

  $this->placeholder(’foo’)->bar = “Some text for later-2″;

  echo $this->placeholder(’foo’);

  $foo = $this->placeholder(’foo’);

  echo $foo[0] .’<br>’;

  echo $foo['bar'] .’<br>’;

?>

<!– Default capture: append –>

<?php

  $this->placeholder(’hoo’)->captureStart();

  foreach ($this->data as $datum)

  {

?>

  <div class=”hoo”>

<h2><?= $datum['title'] ?></h2>

<p><?= $datum['content'] ?></p>

  </div>

<?php

  }

  $this->placeholder(’hoo’)->captureEnd();

?>

<?php

  echo $this->placeholder(’hoo’)

?>

<!– Capture to key –>

<?php

  $this->placeholder(’woo’)->captureStart(’SET’, ‘data’);

  foreach ($this->data as $datum):

?>

  <div class=”woo”>

<h2><?= $datum['title'] ?></h2>

<p><?= $datum['content'] ?></p>

</div>

<?php

  endforeach;

  $this->placeholder(’woo’)->captureEnd();

?>

<?php

  echo $this->placeholder(’woo’)->data;

?>
6.9.4自定義助手
編寫及調用自定義助手的方法:
The class name must, at the very minimum, end with the helper name itself, using MixedCaps. E.g., if you were writing a helper called “specialPurpose”, the class name would minimally need to be “SpecialPurpose”. You may, and should, give the class name a prefix, and it is recommended that you use ‘View_Helper’ as part of that prefix: “My_View_Helper_SpecialPurpose”. (You will need to pass in the prefix, with or without the trailing underscore, to addHelperPath() or setHelperPath()).
The class must have a public method that matches the helper name; this is the method that will be called when your template calls “$this->specialPurpose()”. In our “specialPurpose” helper example, the required method declaration would be “public function specialPurpose()”.
In general, the class should not echo or print or otherwise generate output. Instead, it should return values to be printed or echoed. The returned values should be escaped appropriately.
The class must be in a file named after the helper class. Again using our “specialPurpose” helper example, the file has to be named “SpecialPurpose.php”.
Place the helper class file somewhere in your helper path stack, and Zend_View will automatically load, instantiate, persist, and execute it for you.
示例:
<?php

  //Custom Helper “myFieldset”

  echo $this->myFieldset(’This my custom Helper.’).’<br>’;

  echo $this->myFieldset(’This my custom Helper.’).’<br>’;

  echo $this->myFieldset(’This my custom Helper.’).’<br>’;

?>
調用代碼:function customhelperAction()

{

  $view = new Zend_View();

  $view->setScriptPath(’views’);

  $view->setHelperPath(’views/helpers’, ‘My_View_Helper’);

  echo $view->render(’my_custom_helper.php’);

}
注意:本例只是使用Smarty引擎的其中一種方法。在ZF中還可以用別的形式來使用Smarty模板引擎,我們會在別的章節里介紹。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精品麻豆cm视频在线看 | 免费人成年短视频在线观看网站 | 法国性xxx精品hd | 12av毛片| 欧日韩 | 本色视频aaaaaa一级网站 | 亚洲男人天堂 | 亚洲福利视频52 | 在线成人一区 | 国产午夜精品一区二区三区嫩草 | 精品国产一区二区三区在线观看 | 免费看一级片 | 久久17| 久久久一区二区三区精品 | 蜜桃欧美性大片免费视频 | 日本精品中文字幕 | 中文字幕在线观看精品 | 调教小男生抽打尿孔嗯啊视频 | 欧美精品国产综合久久 | 欧美视频99 | 免费看成人av| 媚药按摩痉挛w中文字幕 | 免费在线观看毛片视频 | 欧美在线观看视频一区二区 | 欧美成人高清视频 | 在线播放视频一区二区 | 日韩视频在线观看免费视频 | 免费欧美精品 | 视频一区二区三区在线观看 | 99爱国产精品 | 国产一区精品视频 | 久久久久久久久久美女 | 在线亚洲欧美日韩 | 免费a视频在线观看 | 看片一区| 国产成人网 | 久久国产精品久久精品国产演员表 | 蜜桃一本色道久久综合亚洲精品冫 | 免费国产一级淫片 | 久久国产精品久久久久久电车 | 久久精品视频在线免费观看 |