我有一个Zend Framework MVC应用程序,其模块结构类似于上面:
/application
/layouts
/sripts
/layout.phtml
/modules
/default
/controllers
/IndexController.php
/OtherController.php
/views
/scripts
/index
/index.phtml
/second.phtml
/third.phtml
/fourth.phtml
/other
/index.phtml
/second.phtml
/third.phtml
/fourth.phtml
在我的layout.phtml中有一行
<div id="main">
<?= $this->layout()->content ?>
</div>
我想要在IndexController和OtherController的每个动作中包装呈现的动作视图,除了fourth之外,使用一些代码,比如在乞讨时使用<div id='top'></div>
,在呈现的动作视图的末尾使用<div id='bottom'></div>
。我不想在每个动作视图*.phtml文件中都手动完成。在实际的应用程序中有太多的代码,而且代码看起来与该解决方案混乱。
该怎么做呢?
发布于 2013-01-04 20:07:17
在布局文件中,可以回显布局变量。这通常是我们放置动作所呈现的html的地方。您可以将来自多个操作的渲染附加到单个布局变量中,它们将以后进先出的顺序显示。下面是如何将变量插入到布局文件中:
<?php echo $this->layout()->myLayoutVariable; ?>
您还可以在布局文件中设置占位符变量:
<?php echo $this->placeholder('myPlaceholderVariable'); ?>
在其中一个视图文件中,您可以提供此占位符变量的html内容:
<?php
$this->placeholder('myPlaceholderVariable')->append('<p>HTML GOES HERE</p>');
?>
如果不为占位符变量设置任何值,布局文件中将不会呈现任何内容。但是如果你为这个占位符变量设置了一个值,它就会被插入到html中。
发布于 2012-05-29 02:35:05
您可以仅为IndexController
和OtherController
设置不同的布局:在每个控制器的init()
方法中,您可以添加以下内容:
Zend_Layout::getMvcInstance()->setLayout('some other layout');
发布于 2012-05-29 02:45:31
试试这个:
$(document).ready(function(){
$("#main").before(//insert header html here);
$("#main").after(//insert footer html here);
});
我不确定你所说的第四个是什么意思,但是如果你想针对一个特定的控制器/动作,你可以抓取window.location.href并使你的函数依赖于你想要查找的一个特定的URL。
希望这能有所帮助。
https://stackoverflow.com/questions/10792446
复制