Zend组织问题
因此,我对 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 中。 有没有办法在不重组我的网站的情况下做到这一点?
如果没有,有没有办法做到这一点,以便我可以:
能够将代码放入布局的不同部分,例如 Layout()-> header、Layout-> footer。
将这些片段分成不同的文件,以便它们易于查找和组织,例如我的index/footer.phtml、index/main.phtml等。
不必将该代码放入引号中以将其转换为字符串以将其传递给 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:
The ability to put code into different sections of my layout, such as Layout()->header, Layout->footer.
Separate these pieces into different files, so that they're easy to find and organize, like my index/footer.phtml, index/main.phtml etc.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个想法:
由于您的布局页眉/页脚现已解析,因此您可以像使用视图一样使用它们。
Here is an idea:
Since your layout headers/footers are now parsed, you can use them just like a view.
$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.如果您使用
它甚至会使用视图,因此在渲染标题时保留定义的所有变量。
我建议
在布局文件中使用类似的内容 - 如果视图脚本不覆盖它,它将从布局文件夹中呈现默认标题。
If you use
It will even use the view, therefore keeping all your variables defined when rendering the header.
I would suggest using something like
in your layout file - it will render a default header from the layout folder if the view script doesn't override it.
您是否尝试过查看查看助手。 它们是将视图逻辑构建为可重用和模块化代码的一种方法。 在这种情况下,您将使用视图助手来生成每个所需的段。 因此,您的示例视图脚本如下所示。
使用视图助手相对于 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
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.
帮手都很好。 另一种选择与上面类似,将文件名放在页眉/页脚中 - 放置模板名称并使用
$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.