aviaPL
aviaPL

Reputation: 99

How to call function in prestashop tpl file

I have Prestashop 1.6. In classes/Product.php there are a function for example:

I want to return date from getImages function in my TPL file. How to do it ? Below I write all code from this function:

/**
* Get product images and legends
*
* @param int $id_lang Language id for multilingual legends
* @return array Product images and legends
*/
public function getImages($id_lang, Context $context = null)
{
    return Db::getInstance()->executeS('
        SELECT image_shop.`cover`, i.`id_image`, il.`legend`, i.`position`
        FROM `'._DB_PREFIX_.'image` i
        '.Shop::addSqlAssociation('image', 'i').'
        LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
        WHERE i.`id_product` = '.(int)$this->id.'
        ORDER BY `position`'
    );
}

Thanks for help. Kind regards.

Upvotes: 1

Views: 2111

Answers (1)

Addis
Addis

Reputation: 314

following your example: with the MVC architecture that Prestashop is based, now you have:

  • The Model -> Product.php, where is the function that pick the values you need.
  • The View -> Is the TPL file where you want to display this values.

So, you need the Controller, what is the responsible to receive the data from the model, prepare it (if it's necessary) and send this data to the template.

Then, you now need to go to the ProductController.php in controllers/front folder.

In the controllers we have a function called initContent() what her function is create and assign the values to the variables that are going to be used in the template.

So, you need to create a new assignment like this:

$this->context->smarty->assign('images_ex', $this->product->getImages((int)$this->context->cookie->id_lang));

And now you can use the variable {$images_ex} in the product.tpl, that is in activated theme root folder.

Hope it helps you.

Upvotes: 1

Related Questions