将变量发送到 Zend Framework 中的布局

发布于 2024-08-06 14:14:46 字数 299 浏览 12 评论 0原文

在我的项目中,我有许多动态元素,它们在每个页面上都是一致的。我已将这些放入我的layout.phtml

我的问题是:如何从我的控制器将变量发送到我的布局中?

如果我想从我的控制器发送东西,我可以使用:

$this->view->whatever = "foo";

并在视图中接收它,

echo $this->whatever;

我不知道如何对我的布局执行相同的操作。也许有更好的方法来解决这个问题?

In my project I have a number of dynamic elements that are consistently on every page. I have put these in my layout.phtml

My question is: How can I send variables into my layout from my controllers?

If I want to send things from my controller I can use:

$this->view->whatever = "foo";

And receive it in the view with

echo $this->whatever;

I cannot figure out how to do the same with my layout. Perhaps there is a better way around the problem?

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

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

发布评论

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

评论(8

看轻我的陪伴 2024-08-13 14:14:46

布局一个视图,因此分配变量的方法是相同的。在您的示例中,如果您要在布局中 echo $this->whatever ,您应该看到相同的输出。

一个常见的问题是如何将每个页面上使用的变量分配给布局,因为您不希望在每个控制器操作中重复代码。解决此问题的一种方法是创建一个插件,在渲染布局之前分配此数据。例如:

<?php

class My_Layout_Plugin extends Zend_Controller_Plugin_Abstract
{
   public function preDispatch(Zend_Controller_Request_Abstract $request)
   {
      $layout = Zend_Layout::getMvcInstance();
      $view = $layout->getView();

      $view->whatever = 'foo';
   }
}

然后向前端控制器注册这个插件,例如

Zend_Controller_Front::getInstance()->registerPlugin(new My_Layout_Plugin());

The layout is a view, so the method for assigning variables is the same. In your example, if you were to echo $this->whatever in your layout, you should see the same output.

One common problem is how to assign variables that you use on every page to your layout, as you wouldn't want to have to duplicate the code in every controller action. One solution to this is to create a plugin that assigns this data before the layout is rendered. E.g.:

<?php

class My_Layout_Plugin extends Zend_Controller_Plugin_Abstract
{
   public function preDispatch(Zend_Controller_Request_Abstract $request)
   {
      $layout = Zend_Layout::getMvcInstance();
      $view = $layout->getView();

      $view->whatever = 'foo';
   }
}

then register this plugin with the front controller, e.g.

Zend_Controller_Front::getInstance()->registerPlugin(new My_Layout_Plugin());

面如桃花 2024-08-13 14:14:46

不使用助手或插件:

Zend_Layout::getMvcInstance()->assign('whatever', 'foo');

此后您可以在布局中使用以下内容:

<?php echo $this->layout()->whatever; ?>

这将打印“foo”。

Without using helpers or plugins do :

Zend_Layout::getMvcInstance()->assign('whatever', 'foo');

After this you can use the following in your layout:

<?php echo $this->layout()->whatever; ?>

This will print "foo".

沉鱼一梦 2024-08-13 14:14:46

我已经实现了一个所有其他控制器都扩展的基本控制器。

所以我有一个控制器......

<?php
class BaseController extends Zend_Controller_Action
{
  public function init()
  {
    $this->view->foo = "bar";
  }
}

并且在布局和/或视图中

<?= $this->foo ?>

I have a implemented a Base Controller which all other controllers extend.

So I have a controller...

<?php
class BaseController extends Zend_Controller_Action
{
  public function init()
  {
    $this->view->foo = "bar";
  }
}

and in the layout and/or view

<?= $this->foo ?>
回忆凄美了谁 2024-08-13 14:14:46

如果您使用 MVC 中的布局,则标准视图变量可用。在引导文件中,包括以下内容:

Zend_Layout::startMvc();

然后,您必须告诉每个控制器(甚至每个操作,如果您想对几个不同的布局进行精细控制)要使用哪个布局。我把我的放在每个控制器的 init() 中。下面是一个示例,如果您的布局文件名为layout.phtml:

$this->_helper->layout->setLayout('layout');

The standard view variables are available if you use the layout within the MVC. In bootstrap file, include this:

Zend_Layout::startMvc();

You must then tell each controller (or even each action, if you wanted granular control over several different layouts) which layout to use. I put mine in the init() of each controller. Here's an example, if your layout file is named layout.phtml:

$this->_helper->layout->setLayout('layout');
吹泡泡o 2024-08-13 14:14:46

好吧,我想您可以通过创建视图助手来获得另一种解决方案。在 application/views/helper 中创建一个文件并将其命名为您想要的 abc.php 然后将以下代码放在那里。

class Zend_View_helper_abc {

    static public function abc() {
        $html = 'YOUR HTML';
        return $html;
    }
}

所以你可以在布局中使用这个助手,例如..

<?= $this->abc() ?>

Well i guess you can have another solution by creating view helper.. create a file in application/views/helper and name it what ever you want abc.php then put the following code over there.

class Zend_View_helper_abc {

    static public function abc() {
        $html = 'YOUR HTML';
        return $html;
    }
}

So you can use this helper in layout like..

<?= $this->abc() ?>
回梦 2024-08-13 14:14:46
class IndexController extends Zend_Controller_Action
{

   public function init()
   {
      $this->_layout = $this->_helper->layout->getLayoutInstance();
      $this->_layout->whatever = $this->view->render('test.phtml);
   }
}

可以在某些操作中调用If :

<p><?php echo $this->layout()->whatever ?>

在布局文件中,如果您不需要该部分,则

public function viewAction()
{
   $this->_layout->whatever = null;
}
class IndexController extends Zend_Controller_Action
{

   public function init()
   {
      $this->_layout = $this->_helper->layout->getLayoutInstance();
      $this->_layout->whatever = $this->view->render('test.phtml);
   }
}

In the layout file you can call

<p><?php echo $this->layout()->whatever ?>

If in some actions if you don't want that section then:

public function viewAction()
{
   $this->_layout->whatever = null;
}
德意的啸 2024-08-13 14:14:46

附带说明一下,如果您在应用程序中的某个时刻发送 json,请注意全局视图变量不会随响应一起发送。

As a side note, if you send json at some point in your app be careful that global view variables are not sent with the response.

2024-08-13 14:14:46

查看助手也是一个好主意。我有一个电子商务网站,其中有一个layout.phtml,其中包含我需要从数据库中获取的类别和子类别的菜单。

为此,我执行了以下操作:

Bootstrap.php:

protected function _initHelperPath() 
{

    $view = $this->bootstrap('view')->getResource('view');

    $view->setHelperPath(APPLICATION_PATH . '/views/helpers', 'View_Helper');

}

application.ini:

resources.view[]=

在视图/帮助程序中,我有一个名为菜单的文件:

class View_Helper_Menus extends Zend_View_Helper_Abstract {

    public function categories(){

       $categories = new Application_Model_DbTable_Categories();

       return $categories->fetchAll();

    }

    public function subCategories(){

        $subCategories = new Application_Model_DbTable_SubCategories();

        return $subCategories->fetchAll();

    }

}

在layout.phtml中,我只需调用特定的帮助程序,并从中调用方法:

$menu = $this->getHelper('Menus');
$categories = $menu->categories();
$subCategories = $menu->subCategories();

希望它可以帮助需要从数据库获取数据来呈现布局的人。

View Helpers are also a good idea. I had a ecommerce website, which I had a layout.phtml with menus with categories and subcategories that I needed to bring from the database.

For this, I did the following:

Bootstrap.php:

protected function _initHelperPath() 
{

    $view = $this->bootstrap('view')->getResource('view');

    $view->setHelperPath(APPLICATION_PATH . '/views/helpers', 'View_Helper');

}

application.ini:

resources.view[]=

In views/helpers, I had a file called Menus:

class View_Helper_Menus extends Zend_View_Helper_Abstract {

    public function categories(){

       $categories = new Application_Model_DbTable_Categories();

       return $categories->fetchAll();

    }

    public function subCategories(){

        $subCategories = new Application_Model_DbTable_SubCategories();

        return $subCategories->fetchAll();

    }

}

In layout.phtml, I just had to call the specific helper, and call the methods from it:

$menu = $this->getHelper('Menus');
$categories = $menu->categories();
$subCategories = $menu->subCategories();

Hope it helps someone that needs to bring data from database to render the layout.

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