如何在 Zend 布局中检索参数

发布于 2024-10-04 14:40:54 字数 285 浏览 0 评论 0原文

我正在阅读《Zend Framework - 初学者指南》一书。 第三章的一部分描述了主布局的使用。

对于我的导航,我想动态设置正文的 id-attrib。如何从任何控制器获取参数到该布局文件?

主布局在 application.ini 中设置:

resources.layout.layoutPath = APPLICATION_PATH "/layouts"
resources.layout.layout = master

greetings 坦率

I'm working through the book "Zend Framework - A beginners guide".
Part of the third chapter describes working with a masterlayout.

For my navigation I'd like to set the id-attrib of the body dynamically. How can I get a parameter from any controller to this layout-file?

The master-layout is set in application.ini:

resources.layout.layoutPath = APPLICATION_PATH "/layouts"
resources.layout.layout = master

greetings
Frank

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

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

发布评论

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

评论(2

心的位置 2024-10-11 14:40:54

您可以将视图变量用于需要传递到布局脚本中的简单变量:

在您的控制器中:

function indexAction()
{
    $this->view->pageTitle = "Zend Layout Example";
}

在您的布局脚本中:

<html>
<head>
    <title><?php echo $this->escape($this->pageTitle); ?></title>
</head>
<body></body>
</html>

You can use view vars for simple variables you need to pass into layout scripts:

In your controller:

function indexAction()
{
    $this->view->pageTitle = "Zend Layout Example";
}

In your layout script:

<html>
<head>
    <title><?php echo $this->escape($this->pageTitle); ?></title>
</head>
<body></body>
</html>
世态炎凉 2024-10-11 14:40:54

做到这一点的最佳方法是使用占位符。下面是一个示例布局:

master.phtml
------------
<html>
   <head>
      <title>My Master Layout</title>
   </head>
   <body id="<?= $this->placeholder('my_dynamic_id_attrib'); ?>">
    ...
   </body>
</html>

请注意,“id”属性的值以“”开头。这与“”相同,如果您使用 Zend 推荐的默认 .htaccess 文件,它应该可以正常工作。如果“”对您不起作用,只需将其替换为:

<body id="<?php echo $this->placeholder('my_dynamic_id_attrib'); ?>">

现在,在您的控制器中,您可以使用以下方法设置动态 id:

IndexController.php
-------------------
public function indexAction(){

  //------------------------------------
  // Can either be $_GET or $_POST, etc.
  $dynamicParam = $this->_getParam('id');

  //------------------------------------
  // Set the dynamic id
  $this->view->placeholder('my_dynamic_id_attrib')->set($dynamicParam);
}

The best way to do this, is to use placeholders. Here's an example layout:

master.phtml
------------
<html>
   <head>
      <title>My Master Layout</title>
   </head>
   <body id="<?= $this->placeholder('my_dynamic_id_attrib'); ?>">
    ...
   </body>
</html>

Note that the value for the "id" attribute starts with "<?=". This is the same as "<?php echo" and it should work correctly if you're using the default .htaccess file which Zend recommends. If "<?=" does not work for you, simply replace it with:

<body id="<?php echo $this->placeholder('my_dynamic_id_attrib'); ?>">

Now, in your controller, you can set your dynamic id using:

IndexController.php
-------------------
public function indexAction(){

  //------------------------------------
  // Can either be $_GET or $_POST, etc.
  $dynamicParam = $this->_getParam('id');

  //------------------------------------
  // Set the dynamic id
  $this->view->placeholder('my_dynamic_id_attrib')->set($dynamicParam);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文