I’m trying to show a sidebar in other pages except, home page, and two other pages,
I’m asking, looking at the code below, am I go about it the recommended way or you have some suggestion for me. Thank you
//templates/layout/default.php
<?php
if (!in_array($this->getRequest()->getParam('controller'), ['Pages', 'Users', 'Another page']))
echo $this->element('sidebar');
echo $this->fetch('user_sidebar');
?>
$this->getRequest()->getParam('controller')
gives you ONLY the name of the used controller, not the action nor other params.
So if you have 3 static pages which are all handled via the PagesController::display
method you will need something like:
$request = $this->getRequest();
$currentController = $request->getParam('controller');
$currentAction = $request->getParam('action');
$pass = $request->getParam('pass');
if($currentController === 'Pages' && currentAction === 'display'){
if ($pass === 'home') {
// do something here
} else if ($pass === 'other') {
// do something else
}
}
6120
February 2, 2023, 10:07pm
3
Why not create another layout ?
Zuluru
February 2, 2023, 10:15pm
4
Or set a “no_sidebar” variable to true in those few exceptions, and show it only if that’s not set? There are many ways to attack this.
this is how I do my sidebars
First adding a fetch in you main layout
$this->fetch('sidebar')
Then simple add view blocks on your templates. Templates without view block sidebar will not load a sidebar.
$this->start('sidebar');
echo $this->element('sidebar/main');
$this->end();
but if you need to check user roles? then you can just make the role as a variable
$this->start('sidebar');
echo $this->element('sidebar/sidebar'.$role);
$this->end();