给你买了一个更好的:
<?php
class My_View_Helper_Attribs extends Zend_View_Helper_HtmlElement
{
public function attribs($attribs) {
if (!is_array($attribs)) {
return '';
}
//flatten the array for multiple values
$attribs = array_map(function($item) {
if (is_array($item) {
return implode(' ', $item)
}
return $item;
}, $attribs);
//the htmlelemnt has the build in function for the rest
return $this->_htmlAttribs($attribs)
}
}
在您的控制器中:
public function indexAction()
{
//notice it is $this->view and not just $this
$this->view->bodyAttribs= array('id' => 'someId', 'class' => array("wide","dark"));
}
public function loginAction()
{
$this->view->bodyAttribs['id'] = "someId2";
$this->view->bodyAttribs['class'] = array();
}
在您的视图脚本中:
<body <?= $this->attribs($this->bodyAtrribs) ?>>