Jump to content

Smarty variable to be accessed globally


CyborgHead

Recommended Posts

I am creating a module on PrestaShop 1.6.

 

I assigned the variable $my_variable to $smarty in my_module.php:

public function hookDisplayTopColumn($parms)
{

    global $smarty;
    $smarty->assign('my_variable', self::get_code());

}

get_code() is a function in my_module class that returns a string.

 

The value inside {$my_variable} is accessible inside my_module.tpl which is hooked to one of PrestaShop's pages. I can display {$my_variable} here without a problem.

 

The problem is {$my_variable} is not available in the template unless I call it inside my_module.tpl, and when I call $smarty->getTemplateVars('my_variable'); inside the controller, it returns 1.

 

I tried to override the FrontController class and called the function

class FrontController extends FrontControllerCore
{
    function init()
    {
        parent::init();
        $my_arr = self::$smarty->getTemplateVars(); 
        print_r($my_arr); 
        
        return parent::displayHeader();
    }
}

The override is working because when I return parent::displayHeader(); the header is displayed twice with all the values assigned to $smarty. However, I was not able to find my assigned variable $my_variable.

 

 

How can I display/access this smarty variable {$my_variable} from a controller and inside the template page where my_module.tpl is hooked?

Link to comment
Share on other sites

As I mentioned, the value inside {$my_variable} can be accessed inside my_module.tpl which is hooked to one of PrestaShop's pages (Contact-us Page).

 

I am able to display {$my_variable} inside the hook without a problem by assigning the return of the static function to $smarty or calling the static function directly in my_module.tpl.

 

The problem is that I need to access $my_variable that is assigned to $smarty in my_module.php, or the static function created in my_module.php inside the ContactController.php which I am overriding.

Link to comment
Share on other sites

Create the hook header in your module and generate right there the smarty variable.

 

my_module is hooked into the top of the contact-us.php.

I am able to generate the smarty variable and display it into my_module.tpl where it is hooked.

 

I need to access this $smarty variable inside the ContactController.php in order to compare it with the user input.

 

I tried the following in the controller and none worked:

$my_variable = MyModule::myFunction();    // Coud not access my function here.
$this->context = Context::getContext();
$my_arr_var = $this->context->smarty->getTemplateVars();
print_r($my_arr_var);    // Printed out everything except what I have assigned to $smarty.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...