Jump to content

Add new global smarty variable


Recommended Posts

Hi,

 

I would like to know how I can add a variable in every smarty template. For example, we can use {$urls.base_url} because $urls is an array that has been assigned for all templates.

 

How can I have {$my_global_variable} available everywhere?

 

I tried a hook in hookActionDispatcher with :

public function hookActionDispatcher($params = []){
	$this->context->smarty->assign('my_global_var', [
        'mycontent' => 'abcdef',
      ]);
}

but it's not working, {$my_global_variable} is an undefined index when I dump($this->context->smarty->tpl_vars['my_global_var']) in my controller:initContent. So I think smarty tpl_vars got reassigned somehow between hookActionDispatcher and controller:initContent

 

Thanks

Florian

Link to comment
Share on other sites

  • 1 year later...

I had the same issue. Just to let others know : there's now a hook for that (since Prestashop 1.7.5.0 apparently). In your module :

public function hookactionFrontControllerSetVariables()
{
	return array(
		'your_variable' => $your_value
	);
}

Then in your template :

${modules.your_module_name.your_variable}

Way better than an override

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 2 months later...
On 6/4/2019 at 3:40 PM, Jgoss said:

I had the same issue. Just to let others know : there's now a hook for that (since Prestashop 1.7.5.0 apparently). In your module :


public function hookactionFrontControllerSetVariables()
{
	return array(
		'your_variable' => $your_value
	);
}

Then in your template :


${modules.your_module_name.your_variable}

Way better than an override

That's extremely helpful, thanks @Jgoss!

 

 

Link to comment
Share on other sites

  • 2 months later...
On 6/4/2019 at 12:40 PM, Jgoss said:

I had the same issue. Just to let others know : there's now a hook for that (since Prestashop 1.7.5.0 apparently). In your module :


public function hookactionFrontControllerSetVariables()
{
	return array(
		'your_variable' => $your_value
	);
}

Then in your template :


${modules.your_module_name.your_variable}

Way better than an override

how can the call you a prestashop newbie ??!!!!

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