Jump to content

Smarty variable in admincontroller


orlangrod

Recommended Posts

Hi all

 

After a deep search on forum, I have not found something similar. So, I proceed to explain.

I'm developing a little module in 1.6.0.11 and I have a little problem, I think.

Mymodule overrides AdminOrdersController.php, it's located in "override/controllers/admin" and it works. In setMedia function added a .js file wich it's located in "modules/mymodule/js/" used to select some element via JQuery. Until here, no problem.

public function setMedia()
{
  parent::setMedia();
  $this->addJs(_PS_MODULE_DIR_.'mymodule/js/mymodule.js');
  $this->addCss(_PS_MODULE_DIR_.'mymodule/css/style.css');
}

Now, I want to be able to retrieve ps configuration table values in my mymodule.js, to open a window with base url for example.

 

I know that variable should be passed to tpl template (from my adminordercontroller.php), something like

public function renderForm()
	{
		$this->context->smarty->assign(array(
			'myvariable' => (int)Configuration::get('mymodule_myvariable'),
		));
		$this->content .= $this->createTemplate('form.tpl')->fetch();
	}

then, I have to define the variable to javascript in tpl file, how and where? "/admin/themes/default/template/controllers/orders/form.tpl"?

 

Thanks in advantage

Link to comment
Share on other sites

In the tpl add:

<script type="text/javascript">

var myjavascriptvariable = "{$myvariable}";

</script>

 

Now "myjavascriptvariable" will be a JavaScript variable with the smarty variable content.

 

in "/admin/themes/default/template/controllers/orders/form.tpl"?

I tried in that one, but it's not working.

Link to comment
Share on other sites

Thanks for your help COTOKO

I added

console.log(myjavascriptvariable);

in file "modules/mymodule/js/myjs.js" and it returns ReferenceError: myjavascriptvariable is not defined in console.

 

I think the problem would be related to location of files?

 

My AdminOrdersController.php is located in "override/controllers/admin" and javascript file added in setmedia function in "modules/mymodule/js/myjs.js"

 

I added this in "/admin/themes/default/template/controllers/orders/form.tpl" but I can't find the error.

<script type="text/javascript">
var myjavascriptvariable = "{$myvariable}";
</script>
Link to comment
Share on other sites

Well it is clear that the files are not being executed in the same place and therefore there is no variable, if you are using a module, there is a hook named "displayBackOfficeHeader" to add JS or CSS files, also you can use hooks in modules to generate smarty code in the back office, maybe this way can be more easy for you.

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for your help.

 

I solved this by using  hookDisplayBackOfficeHeader in the main class of my module.

public function hookDisplayBackOfficeHeader($params)
{
$this->context->smarty->assign(
      array(
          'myvar' => 'example',
      ));

$this->context->controller->addJS(($this->_path).'js/mymodule.js');

return $this->display(__FILE__, 'mymodule.tpl');
}

in mymodule.tpl

{strip}
{addJsDef myvar=$myvar}
{/strip}

then, I was able to use myvar in mymodule.js

  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...

Hi everyone !

 

My question is quite similar to the topic and it's very basic - I think :)

 

I would like to see the list of variables / arrays / objects in the smarty : /admin/themes/default/template/controllers/orders/heleprs/view/view.tpl

 

I've used the command {debug} in the begining of /admin/themes/default/template/header.tpl file and I can see the Smarty debug console.

 

But this list doesn't contain the variables assigned from view.tpl, for example there is no $customers

 

Where I can see the list of such variables ?

 

Thanks for reply.

Link to comment
Share on other sites

  • 2 years later...
On 22/1/2015 at 7:15 PM, orlangrod said:

Thanks for your help.

 

I solved this by using  hookDisplayBackOfficeHeader in the main class of my module.


public function hookDisplayBackOfficeHeader($params)
{
$this->context->smarty->assign(
      array(
          'myvar' => 'example',
      ));

$this->context->controller->addJS(($this->_path).'js/mymodule.js');

return $this->display(__FILE__, 'mymodule.tpl');
}

in mymodule.tpl


{strip}
{addJsDef myvar=$myvar}
{/strip}

then, I was able to use myvar in mymodule.js

Worked also for me :D

Link to comment
Share on other sites

  • 1 year later...
On 1/12/2015 at 7:55 PM, Rolige said:

In the tpl add:

<script type="text/javascript">

var myjavascriptvariable = "{$myvariable}";

</script>

 

Now "myjavascriptvariable" will be a JavaScript variable with the smarty variable content.

var myjavascriptvariable = "{$myvariable}";
Its working only for string. When I assign here array for use in js file it show empty noting gone on js file

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