还有一篇关于Stack Overflow的文章,其中包括以下基于产品ID 提供多个产品模板的代码
//42 is the id of the product
if ($this->request->get['product_id'] == 42) {
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/customproduct.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/customproduct.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
} else {
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
$this->template = $this->config->get('config_template') . '/template/product/product.tpl';
} else {
$this->template = 'default/template/product/customproduct.tpl';
}
}
我想检查一个我不会使用的替代产品字段值,而不是ID,这样它就可以从管理面板中进行管理。
例如,一条语句,上面写着"如果产品位置=附件,那么获取产品/附件.tpl"
我是否必须在产品控制器中加载该字段,然后才能用if语句请求它?
语法会是什么样子?
您应该能够在管理面板中使用产品数据中的任何字段,例如您已经引用的Location。
请求行的product
表中的所有内容都应出现在$product_info
数组中。
试试这样的东西:
$template = ($product_info['location'] == 'accessory') ? 'accessory.tpl' : 'product.tpl';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
如果您预计会有许多不同的模板用于不同的位置,那么使用交换机控制会更有效。
switch ($product_info['location']):
case 'accessory':
$template = 'accessory.tpl';
break;
case 'tool':
$template = 'tool.tpl';
break;
default:
$template = 'product.tpl';
break;
endswitch;
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/' . $template)) {
$this->template = $this->config->get('config_template') . '/template/product/' . $template;
} else {
$this->template = 'default/template/product/' . $template;
}
希望能有所帮助。