热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

走进ZendFramework框架编程6(视图)

视图部分内容包括:视图,模板,视图帮助类等。6.0视图介绍在Zendframework的MVC编程模型中,视图(View)是在控制器的控制和指挥下,用来对程序逻辑进行呈现(Render)的。呈现的结果,就是我们在浏览器里看到的文字、图片、表单等各种网页元素及其字体、颜色、样式等各种效果。Zend_ViewClass就是负责视图工作的类,它有效地完
视图

部分内容包括:视图,模板,视图帮助类等。

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’, )语句的再解释
Index文件中$fc->setParam(’noViewRenderer’, );语句和视图的某性特性有很大关系,比如视图存放的路径等一些默认属性都受该开关属性的影响。
false 是noViewRenderer的默认值。也就是说,如果没有该语句,则认为noViewRenderer参数被设置为false。
noViewRenderer参数被设置为false:意味着控制器使用ZF默认的视图特性,比如视图文件默认必须存放于views\script\\文件夹下,视图文件必须存在,而且其名字必须为.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视图脚本模板文件内容:

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

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

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

?>
myclass.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模板系统的详细用法请参考其帮助文档。
示例:

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文件内容:

if ($this->books):

?>

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

?>

作者 书名
escape($val['author']) ?> escape($val['title']) ?>

else:

?>

There are no books to display.

endif;
eachbook.tpl文件内容:

{author}

{title}


6.8视图脚本的模板系统—使用 Zend_View_Interface调用第三方模板引擎
我们还可以通过实现Zend_View_Interface接口,来得到一个可用的模板系统。
ZF对Zend_View_Interface接口的原始定义:/** Return the actual template engine object */
http://www.cncms.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文件中,该文件的内容如下:

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 {@link assign()} or

* property overloading ({@link __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自带的助手
示例代码:

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)) .’
’;

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

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

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

echo $this->formText(’email’, ‘you@example.com’, array(’size’ => 32)) .’
’;

echo ‘Your Country:’;

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

echo ‘Would you like to opt in?’;

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

echo ‘Choose them:’;

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

echo ‘Choose one:’;

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

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

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

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

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

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

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

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

?>
6.9.2动作(Action)助手
允许我们在视图脚本里执行某个控制器里的动作方法。
示例:

action(’assign1′, ‘Book’); ?>

6.9.3区域(Partial)助手
区域助手的基本用法是在它自己的视图范围内解析另一个模板的片段,类似视图脚本嵌套调用。
区域助手的基本用法:
示例:
booklist.php文件包含脚本片段:

if ($this->books):

?>

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

?>

作者 书名
escape($val['author']) ?> escape($val['title']) ?>

else:

?>

There are no books to display.

endif;

?>
在另一个视图脚本通过Partial助手调用booklist.php文件:
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》’),

)));

?>

’; ?>

$model = array(

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

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

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

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

);

?>

partialLoop(’partialLoop.phtml’, $model) ?>

使用 PartialLoop 来解析可迭代的(Iterable)的模型
示例:

$model = array(

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

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

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

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

);

?>

partialLoop(’partialLoop.phtml’, $model) ?>

以上代码中partialLoop 调用了以下partialLoop.phtml的内容:

key ?>

value ?>

6.9.4占位(Placeholder)助手
示例:
doctype(’XHTML1_STRICT’) ?>

// 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();

?>

$this->placeholder(’foo’)->setPrefix(”

    \n
  • ”)

      ->setSeparator(”

  • \n”)

      ->setIndent(4)

      ->setPostfix(”

\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] .’
’;

echo $foo['bar'] .’
’;

?>

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

foreach ($this->data as $datum)

{

?>

}

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

?>

echo $this->placeholder(’hoo’)

?>

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

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

?>

endforeach;

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

?>

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.
示例:

//Custom Helper “myFieldset”

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

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

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

?>
调用代码: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模板引擎,我们会在别的章节里介绍。


推荐阅读
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
author-avatar
林伯爵
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有