简体   繁体   English

在YII中删除特定页面的页眉和页脚的最佳方法

[英]Best way to remove header and footer for a particular page in YII

I came across a situation where I don't need to show the header (it has the navigator bar and logo) and footer` for a particular page in yii. 我遇到了一种情况,我不需要在yii中显示特定页面的header (它有导航栏和徽标) and页脚`。 There are a number of tweaks to do this. 有很多调整可以做到这一点。 For example, having a check for a particular action where the layout will not load header and footer.. 例如,检查布局不会加载页眉和页脚的特定操作。

Does yii provide any thing solid for this issue? yii是否为此问题提供了任何可靠的东西?

Do we have anything like $this->layout->unsetHeader();? 我们有类似$this->layout->unsetHeader();?东西$this->layout->unsetHeader();? or $this->layout->unsetFooter();? $this->layout->unsetFooter();?

I don't want to have a check in the layout file to sort this out. 我不想在布局文件中检查以对其进行排序。

Help would be appreciated. 帮助将不胜感激。

You can set $this->layout to false; 您可以将$ this-> layout设置为false;

$this->layout = false
$this->render('view', $data);

This can also be achieved using renderpartial 这也可以使用renderpartial来实现

$this->renderPartial('view', $data);

The approach above will display a page segment and not other items of the page layout (for example inclusion of javascript and css, and HTML headers). 上面的方法将显示页面段而不是页面布局的其他项(例如包含javascript和css,以及HTML标头)。

If you want your layout files to have logic in it, you can pass variables from the controller. 如果希望布局文件中包含逻辑,则可以从控制器传递变量。

$this->layout = 'mytheme';
$this->show_css = true;
$this->render('view', $data);

and in your layout 并在你的布局

<html>
<body>
   <?php if ($this->show_css) { ?>
   <div id="menu">
     <ul><li>Menu Option 1</li><li>Menu Option 2</li></ul>
   </div>
   <?php } <?
   <div id="body">
      <?php echo $this->content; ?>
   </div>
   <div id="footer">
   </div>
</body>
</html>

If you don't want to use a variable in the controller. 如果您不想在控制器中使用变量。 use a global variable instead. 改为使用全局变量。

   Yii::app()->params['show_menu'] = true;

I haven't find function like you looking for. 我找不到像你一样的功能。

I think the best and siplest way is however use a proper layout, and call it in controller in this way 我认为最好和最简单的方法是使用适当的布局,并以这种方式在控制器中调用它

public function actionYourView()
   {

    $this->layout='yourLayout';
    $this->render('yourView');
}

In this way you don't need check inside the layout for header and footer the logic is all in the controller. 通过这种方式,您不需要在页面和页脚的布局内检查逻辑是否在控制器中。 And assign the proper layout is easy. 并且分配合适的布局很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM