我想知道有没有办法让这段代码变得更加微妙?
前
public function hookDisplayHeader()
{
if(Tools::getValue('controller')=='index'){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}
if(Tools::getValue('controller')=='cms'){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}
if(Tools::getValue('controller')=='product'){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}
if(Tools::getValue('controller')=='category'){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}
}
简单
public function hookDisplayHeader()
{
if(Tools::getValue('controller')=='index AND product AND cms AND category'){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}
}
此代码不起作用:(
答案 0 :(得分:2)
使用in_array();
public function hookDisplayHeader()
{
$values = array('index','cms','product','category');
if(in_array(Tools::getValue('controller'), $values)){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}
}
答案 1 :(得分:1)
我会将它们放在一个数组中并检查:
$somethingCSSControllers = array('index','product','cms','category');
if(in_array(Tools::getValue('controller'),$somethingCSSControllers)){
$this->context->controller->addCSS(($this->_path).'something.css', 'all');
}