简体   繁体   English

根据页面更改 CSS class

[英]Changing CSS class dependent on page

Using Zend, I'm trying to find the most efficient way to call a new CSS class for each page.使用 Zend,我试图找到最有效的方法来为每个页面调用一个新的 CSS class。 I would rather not create a new layout for each page.我宁愿不为每个页面创建新的布局。 I was curious if I could use a variable to change the class.我很好奇是否可以使用变量来更改 class。

In the layout I was going to use: <div id="main" class="<?php echo $header; ?>">在我要使用的布局中: <div id="main" class="<?php echo $header; ?>">

The CSS looks like: CSS 看起来像:

#main {
display:block;
min-width:960px;
}

#main.homeBG {
height:451px;
background:url(../img/images/home_bg.jpg) no-repeat center top;
}

#main.toolsBG {
height:451px;
background:url(../img/images/tools_bg.jpg) no-repeat center top;
}

So there are different BG images for the different pages.所以不同的页面有不同的BG图像。

What I am trying to do is change the $header variable in the Controller action for each view.我要做的是更改每个视图的 Controller 操作中的 $header 变量。

class ToolsController extends ZendExtension_Controller_Action
{
/**
 * Init
 */
public function init()
{
}
/**
 * Default Index Action
 */
public function indexAction()
{
    $header = $this->view->('toolsBG');
}
}

I know that's wrong, but I'll just use that for my example here.我知道那是错误的,但我将在这里举个例子。

Would there be a way to accomplish this, or should I just make a separate layout page for each view?有没有办法做到这一点,或者我应该为每个视图制作一个单独的布局页面?

Thanks.谢谢。

If your question is how to set a view variable from controller, then this is how:如果您的问题是如何从 controller 设置视图变量,那么方法如下:

//controller
$this->view->header = 'toolsBG';

//view
<div id="main" class="<?php echo $this->header; ?>">

But if your action names are self explanatory then you can create a view helper and within that helper, you can return your action name, or an underscore-separated combination of module, controller, action etc.但是,如果您的操作名称是不言自明的,那么您可以创建一个视图帮助程序,并且在该帮助程序中,您可以返回您的操作名称,或者模块、controller、操作等的下划线分隔组合。

For info on how to create a view helper, see this有关如何创建视图助手的信息,请参阅

For info on how to get module, controller, action in view helper, use the following:有关如何获取模块 controller 的信息,视图助手中的操作,请使用以下内容:

$request    = Zend_Controller_Front::getInstance()->getRequest();
$module     = $request->getModuleName();
$controller = $request->getControllerName();
$action     = $request->getActionName();

I am not into Zend Framework, but are you trying to set the $header variable to the current page?我不喜欢 Zend 框架,但是您是否尝试将 $header 变量设置为当前页面? If so, try using $_SERVER['REQUEST_URI'] (yes, ugly) or maybe pass it on as a variable into your view?如果是这样,请尝试使用$_SERVER['REQUEST_URI'] (是的,丑陋的)或者将其作为变量传递到您的视图中?

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

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