Zend组织问题

发布于 2024-07-24 02:58:06 字数 1215 浏览 6 评论 0原文

因此,我对 Zend 框架的布局代码的一般组织有疑问。

我的布局基本上是这样的:

(LAYOUT.PHTML)

<div id='header'>
<?= $this->Layout()->header ?>
</div>

<div id='main'>
<?= $this->Layout()->main ?>
</div>

<div id='footer'>
<?= $this->Layout()->footer ?>
</div>

等等。 现在,为了将页眉中的代码与主代码和页脚代码分开,我为视图创建了一个文件夹,其中包含 header.phtml、main.phtml、footer.phtml。 然后,我使用此代码将 header.phtml 的内容分配到 $this->layout()->header:

(INDEX.PHTML)

$this->Layout()->header = file_get_contents('index/header.phtml');
$this->Layout()->main = file_get_contents('index/main.phtml');
$this->Layout()->footer = file_get_contents('index/footer.phtml');

这很好用,但我已经遇到了一个问题,我不希望 main 成为静态 HTML不再了。 我希望能够使用 PHP 插入一些值。 因此,在indexAction 的控制器中,我希望能够从数据库加载并将值放入index/main.phtml 中。 有没有办法在不重组我的网站的情况下做到这一点?

如果没有,有没有办法做到这一点,以便我可以:

  1. 能够将代码放入布局的不同部分,例如 Layout()-> header、Layout-> footer。

  2. 将这些片段分成不同的文件,以便它们易于查找和组织,例如我的index/footer.phtml、index/main.phtml等。

  3. 不必将该代码放入引号中以将其转换为字符串以将其传递给 Layout()-> ;header 等。

非常感谢你们的帮助。

-伊森

So I had a question on general organization of code for the Zend framework with regard to the layout.

My layout is basically this:

(LAYOUT.PHTML)

<div id='header'>
<?= $this->Layout()->header ?>
</div>

<div id='main'>
<?= $this->Layout()->main ?>
</div>

<div id='footer'>
<?= $this->Layout()->footer ?>
</div>

and so on and so forth. Now, in order to keep my code in my header separate from the code of my main and the code of my footer, I've created a folder for my view that holds header.phtml, main.phtml, footer.phtml. I then use this code to assign the content of header.phtml into $this->layout()->header:

(INDEX.PHTML)

$this->Layout()->header = file_get_contents('index/header.phtml');
$this->Layout()->main = file_get_contents('index/main.phtml');
$this->Layout()->footer = file_get_contents('index/footer.phtml');

That was working great, but I've hit a point where I don't want main to be static HTML anymore. I would like to be able to insert some values with PHP. So in my Controller in indexAction, I want to be able to load from my database and put values into index/main.phtml. Is there a way to do this without restructuring my site?

If not is there a way to do it so that I can have:

  1. The ability to put code into different sections of my layout, such as Layout()->header, Layout->footer.

  2. Separate these pieces into different files, so that they're easy to find and organize, like my index/footer.phtml, index/main.phtml etc.

  3. Not have to put that code into quotes unnecessarily to turn it into a string to pass it to Layout()->header etc.

Thank you guys so much for your help.

-Ethan

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

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

发布评论

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

评论(5

坏尐絯 2024-07-31 02:58:06

这是一个想法:

  • 将布局()-> header 指定为文件名而不是内容。
  • 将您的代码放入此文件中
  • 在布局文件中,include() 或 require() 布局 -> header()。

由于您的布局页眉/页脚现已解析,因此您可以像使用视图一样使用它们。

Here is an idea:

  • Assign layout()->header the filename instead of the contents.
  • Put your code in this file
  • In your layout file, include() or require() the layout->header().

Since your layout headers/footers are now parsed, you can use them just like a view.

ぃ弥猫深巷。 2024-07-31 02:58:06

$this->layout()->header 中的 ->header响应段。 您可以在操作中使用 $this->_helper->viewRenderer->setResponseSegment('header'); 渲染部分响应。

The ->header in $this->layout()->header is response segment. You can render parts of response using $this->_helper->viewRenderer->setResponseSegment('header'); in an action.

我还不会笑 2024-07-31 02:58:06

如果您使用

$this->layout()->header = $this->render('index/header.phtml');

它甚至会使用视图,因此在渲染标题时保留定义的所有变量。

我建议

<?php echo ($header = $this->layout()->header)? 
      $header : $this->render('headerDefault.phtml'); ?>

在布局文件中使用类似的内容 - 如果视图脚本不覆盖它,它将从布局文件夹中呈现默认标题。

If you use

$this->layout()->header = $this->render('index/header.phtml');

It will even use the view, therefore keeping all your variables defined when rendering the header.

I would suggest using something like

<?php echo ($header = $this->layout()->header)? 
      $header : $this->render('headerDefault.phtml'); ?>

in your layout file - it will render a default header from the layout folder if the view script doesn't override it.

趴在窗边数星星i 2024-07-31 02:58:06

您是否尝试过查看查看助手。 它们是将视图逻辑构建为可重用和模块化代码的一种方法。 在这种情况下,您将使用视图助手来生成每个所需的段。 因此,您的示例视图脚本如下所示。

$this->Layout()->header = $this->header();
$this->Layout()->main = $this->main();
$this->Layout()->footer = $this->footer();

使用视图助手相对于 include 和 require 语句的好处是,所有文件处理和名称解析都由框架处理。 手册提供了有关如何设置路径的更多信息以及使用示例等

Have you tried looking at view helpers. They are a way of structuring view logic into reusable and modular code. In this case you would use a view helper to generate each of your required segments. So your example view script would look like

$this->Layout()->header = $this->header();
$this->Layout()->main = $this->main();
$this->Layout()->footer = $this->footer();

The benefit of using view helpers over include and require statements is that all of the file handling and name resolution is handled by the framework. The manual has more information on how to set up the paths and usage examples etc.

清风不识月 2024-07-31 02:58:06

帮手都很好。 另一种选择与上面类似,将文件名放在页眉/页脚中 - 放置模板名称并使用 $this->render($this->layout()->header)) 等等等。这就像上面的 include/require 一样,但更加一致。

helpers are good. Another option is like the above, putting filenames in header/footer - put the template names and use $this->render($this->layout()->header)), etc etc. This is just like the include/require above, but more consistent.

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