简体   繁体   中英

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. I would rather not create a new layout for each page. I was curious if I could use a variable to change the class.

In the layout I was going to use: <div id="main" class="<?php echo $header; ?>">

The CSS looks like:

#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.

What I am trying to do is change the $header variable in the Controller action for each view.

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
$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.

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:

$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? If so, try using $_SERVER['REQUEST_URI'] (yes, ugly) or maybe pass it on as a variable into your view?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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