orlangrod Posted January 12, 2015 Share Posted January 12, 2015 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 More sharing options...
Rolige Posted January 12, 2015 Share Posted January 12, 2015 In the tpl add: <script type="text/javascript"> var myjavascriptvariable = "{$myvariable}"; </script> Now "myjavascriptvariable" will be a JavaScript variable with the smarty variable content. Link to comment Share on other sites More sharing options...
orlangrod Posted January 12, 2015 Author Share Posted January 12, 2015 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 More sharing options...
Rolige Posted January 12, 2015 Share Posted January 12, 2015 1).- Be sure that the smarty variable is correctly.2).- Use console.log(myjavascriptvariable); to see if variable is created correctly.3).- Clear all the cache, maybe the form.tpl is cached and new changes are no affected. Link to comment Share on other sites More sharing options...
orlangrod Posted January 12, 2015 Author Share Posted January 12, 2015 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 More sharing options...
Rolige Posted January 13, 2015 Share Posted January 13, 2015 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 More sharing options...
orlangrod Posted January 22, 2015 Author Share Posted January 22, 2015 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 1 Link to comment Share on other sites More sharing options...
reno123 Posted March 17, 2015 Share Posted March 17, 2015 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 More sharing options...
Kogkalidis Posted November 4, 2017 Share Posted November 4, 2017 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 Link to comment Share on other sites More sharing options...
anuj.anuj555 Posted November 12, 2018 Share Posted November 12, 2018 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now