为什么在模板文件中变量的属性是通过点(.)获取的,而添加的变量是通过箭头(->)获取的。在 mainmenu.tpl 我有:
public function renderWidget($hookName, array $configuration)
{
$this->smarty->assign([
'menu' => $this->getWidgetVariables($hookName, $configuration),
]);
// assign new variable
$id = 661;
$id_lang = $this->context->language->id;
$product = new Product((int) $id, false, (int) $id_lang);
$this->smarty->assign('product', $product);
return $this->fetch('module:ps_mainmenu/ps_mainmenu.tpl');
}
在 ps_mainmenu.tpl 中:
{$menu.label}
{$product->name}
{$product.name} //not render
如何通过点访问每个属性?
(.) 访问器在引用关联数组的属性时使用,并且在这种类型中传递了大多数 prestashop 对象。所以对象应该被转换为数组:
$this->smarty->assign('product', (array)$product);
我的镜头, 你也应该
$this->smarty->assign('product', $product);
改为
$this->smarty->assign('product' => $product);
顺便说一句
$product
它不是全球性的,所以可以从任何地方轻松访问吗?