How to switch theme over category in Zend Framework?
first, I don't need to "switch" layout.
I'm speaking of CSS theme, that means the layout remains the same.
What I need to do is sw开发者_如何学Goitch a css class when on certains category.
Category are defined by the route (name & id in request params)
So I think of a ViewHelper, used in my layout.phtml
to set a <body class="theme-category"></body>
But I'll to access the request object in my view helper, is it possible? Is it best practice?
Secondly I though to use the same process but pass the parameters in my controller, and fallback on a default one in my helper if none provided.
What are you feedbacks?
In your layout put this
<?=$this->headLink(); ?>
And in the controllers you can add a specific css file like this
$this->view->headLink()->appendStylesheet("/css/file.css");
That will enable you to change the css theme on every controller. P.S. add it in the init function of the controller.
What about a helper method that will just returns the css class, you call this one from your controller, and just pass the css value to the view ?
class YourController {
public function yourpageAction(){
// do your stuff, then call your helper
$this->view->bodyCssClass = $this->setcss();
}
protected function setcss(){
// analyse here all the parameter you wish (route, name, id etc.)
// and just return the correct css string
return $css;
}
}
You may even use the "init()" method to call automatically your "setcss" method (or whatever the name you give it). To go further you may extend the Zend Controller class, and have all you controller extend your specific controller : all pages will inherit this css defining process. And it helps to maintain and read your code.
And in your view, just a simple call :
<body class="<?php echo $this->bodyCssClass;">
精彩评论