How To Access Variable in tpl files Prestashop

It's not that simple to access variable in header.tpl especially if you are beginner with Prestashop. To achive this task you need to override the Prestashop FrontController and there you can assign to smarty a new variable then you can call it in header.tpl.

To do that you have to go in: override -> controllers -> front, create a new file called FrontController.php and put the following code in it.

<?php

class FrontController extends FrontControllerCore{
  function init(){
    parent::init();
    $my_var = Configuration::get('my_option'); 
    self::$smarty->assign('my_option_name', $my_var);
  }
}
 

Now smarty will know about this variable, to call it in header.tpl you need to use the following line of code:

{var_dump($theme_facebook_url)};

Now you will can see the output for this variable. There is no problem with this function because you only override the core controller so there will be no problem with your code. If you have multiple options to assign you can assign directly an array using the following function:

function init(){
    parent::init();
    $var1 = Configuration::get('option_1_name'); 
    $var2 = Configuration::get('option_2_name');
    $var3 = Configuration::get('option_3_name');
    self::$smarty->assign(
      array(
        'assigned_var_1'   => $var1,
        'assigned_var_2'   => $var2,
        'assigned_var_3'   => $var3 
      )
    );
  }

Now all these variables will be accesible all over your main tpl files. If you have any problems with this code let the issue in comments section and I will try to help you.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Manuel
Manuel
8 months ago

I have problem trying to display variables in friendly url
how do I solve it?
I want them to be shown in the cms pages
Thank you

1
0
Would love your thoughts, please comment.x
()
x