我应该在 Zend Framework 中的哪里找到与布局相关的逻辑?

发布于 2024-11-15 22:48:47 字数 1345 浏览 3 评论 0原文

我需要自定义 body 标签的属性。我应该把逻辑放在哪里?在基本控制器中,查看 Helper ?

这应该是布局

<?=$this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        ...
    </head>
    <body<?=$this->bodyAttrs?>>  <!-- or <?=$this->bodyAttrs()?> -->
        ...
    </body>
</html>

这应该是控制器中的变量声明

class Applicant_HomeController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->idBody = "someId1";
        $this->classesBody = array("wide","dark");
    }

    public function loginAction()
    {
        $this->idBody = "someId2";
        $this->classesBody = array();
    }

    public function signUpAction()
    {
        $this->idBody = "someId3";
        $this->classesBody = array("no-menu","narrow");
    }
}

这是连接属性的函数。

/**
  * @param string $idBody     id Attribute
  * @param array $classesBody class Attribute (array of strings)
  */
protected function _makeBodyAttribs($idBody,$classesBody)
{
    $id = isset($idBody)?' id="'.$idBody.'"':'';
    $hasClasses = isset($classesBody)&&count($classesBody);
    $class = $hasClasses?' class="'.implode(' ',$classesBody).'"':'';
    return $id.$class;
}

我需要最后的胶水代码。

I need to customize the attributes of my body tag. Where should I locate the logic? In a Base Controller, view Helper ?

This should be the layout

<?=$this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        ...
    </head>
    <body<?=$this->bodyAttrs?>>  <!-- or <?=$this->bodyAttrs()?> -->
        ...
    </body>
</html>

And this should be the variables declaration in controllers

class Applicant_HomeController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->idBody = "someId1";
        $this->classesBody = array("wide","dark");
    }

    public function loginAction()
    {
        $this->idBody = "someId2";
        $this->classesBody = array();
    }

    public function signUpAction()
    {
        $this->idBody = "someId3";
        $this->classesBody = array("no-menu","narrow");
    }
}

This is the function where the attributes are concatenated.

/**
  * @param string $idBody     id Attribute
  * @param array $classesBody class Attribute (array of strings)
  */
protected function _makeBodyAttribs($idBody,$classesBody)
{
    $id = isset($idBody)?' id="'.$idBody.'"':'';
    $hasClasses = isset($classesBody)&&count($classesBody);
    $class = $hasClasses?' class="'.implode(' ',$classesBody).'"':'';
    return $id.$class;
}

I need the last glue code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-11-22 22:48:47

为你提供了一个更好的:

<?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) ?>>

Got one better for ya:

<?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)
    }
}

in your controller:

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

in your view script:

<body <?= $this->attribs($this->bodyAtrribs) ?>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文