Jump to content

Create a new Global Variable/constant for use in all TPL


dianacd

Recommended Posts

Hello!! I want to create some custom global constants that I can access from all TPL, this is some information like specific emails or external URLs that I want to put across the webpage, and in case that they change I just need to update the global constant and not all the TPLs that contain the information.

 

Is there a way to create a custom global constant?

Link to comment
Share on other sites

hello, its possible using two different methods
1st is by override the front controller "classes/controller/FrontController.php" and assign your new variable in function assignGeneralPurposeVariables
by using templateVars array.

second is using hook, for this you need to make small module and use hook actionFrontControllerSetVariables

Example:

public function hookactionFrontControllerSetVariables()
{
    $new_variable = 'hello world';
    return array(
        'new_variable' => $new_variable
    );
}

And you can use this in template like this:

${modules.module_name.new_variable}

Link to comment
Share on other sites

3 hours ago, Razi said:

hello, its possible using two different methods
1st is by override the front controller "classes/controller/FrontController.php" and assign your new variable in function assignGeneralPurposeVariables
by using templateVars array.

second is using hook, for this you need to make small module and use hook actionFrontControllerSetVariables

Example:

public function hookactionFrontControllerSetVariables()
{
    $new_variable = 'hello world';
    return array(
        'new_variable' => $new_variable
    );
}

And you can use this in template like this:

${modules.module_name.new_variable}

In tpl:

{$new_variable}

Link to comment
Share on other sites

8 hours ago, Razi said:

hello, its possible using two different methods
1st is by override the front controller "classes/controller/FrontController.php" and assign your new variable in function assignGeneralPurposeVariables
by using templateVars array.

second is using hook, for this you need to make small module and use hook actionFrontControllerSetVariables

Example:

public function hookactionFrontControllerSetVariables()
{
    $new_variable = 'hello world';
    return array(
        'new_variable' => $new_variable
    );
}

And you can use this in template like this:

${modules.module_name.new_variable}

Thanks for your advice! I did it in the fast way, I override the FrontController and it works as expected :)

Link to comment
Share on other sites

  • 3 months later...
On 11/1/2020 at 6:36 PM, dianacd said:

Finally I created a module to set my global variables. They are accesible only from TPL files though. Here is the code in case that it may be useful for anyone:

https://github.com/dianacd/prestashop_modules/tree/master/mcr_global_var

 

You can access from .tpl with {$your_variable1} ?
I am trying to send variables from a module to the invoice template. I can't get it to work, I'm doing something wrong: S
I have also tried to load these variables into $data array of "HTMLTemplateInvoice.php", trying to save from the module as smarty variable, but I also can't

edit:
By doing tests, with the override I can show the variable in the theme's .tpl, but not in the invoice tpl.
With the module method I can't, I have tested the functions; "hookDisplayPDFInvoice ()" and "hookactionFrontControllerSetVariables ()"
what am I missing?

Edited by anderrios (see edit history)
Link to comment
Share on other sites

19 hours ago, anderrios said:

You can access from .tpl with {$your_variable1} ?
I am trying to send variables from a module to the invoice template. I can't get it to work, I'm doing something wrong: S
I have also tried to load these variables into $data array of "HTMLTemplateInvoice.php", trying to save from the module as smarty variable, but I also can't

edit:
By doing tests, with the override I can show the variable in the theme's .tpl, but not in the invoice tpl.
With the module method I can't, I have tested the functions; "hookDisplayPDFInvoice ()" and "hookactionFrontControllerSetVariables ()"
what am I missing?

You can access from .tpl using {$modules.mcr_global_var.your_variable}

 

To use my code you can download the only the mcr_global_var.php file and compress it into a folder and install it as a module from the BO, or just add it manually to your modules folder, then you can update the files from the modules folder with your variables anytime.

 

 

Link to comment
Share on other sites

39 minutes ago, dianacd said:

You can access from .tpl using {$modules.mcr_global_var.your_variable}

To use my code you can download the only the mcr_global_var.php file and compress it into a folder and install it as a module from the BO, or just add it manually to your modules folder, then you can update the files from the modules folder with your variables anytime.

Thank you very much for your quick reply, I managed to get it to work with your module in the theme's .tpl, but I still can't get it to work in "invoice.addresses-tab.tpl".
Maybe I have to first bring the information to "HTMLTemplateInvoice.php" and load it into the $ data array?

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...