Prestashop 1.7 Smarty engine passing variable from controller to .tpl file

Viewed 163

Why in the template file variable's properties are get by dot (.) but added variables are get by arrow (->). In mainmenu.tpl I have:

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');
}

and in ps_mainmenu.tpl:

    {$menu.label}
    {$product->name}
    {$product.name} //not render

How to acccess every property by dot?

1 Answers

The (.) accessor is used when referencing properties of associative array, and in this type are passed most of prestashop objects. So object should be casted to array:

$this->smarty->assign('product', (array)$product);
Related