Zend Framework 中布局相关的逻辑应该放在哪里?

问题描述 投票:0回答:1
php zend-framework zend-view zend-layout
1个回答
2
投票

给你买了一个更好的:

<?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) ?>>
© www.soinside.com 2019 - 2024. All rights reserved.