1

I'm creating a CS-Cart v4.10 addon.

I want to execute some Javascript code from inside the scripts.post.tpl template hook. I figured this hook out from taking a look at other addons, because actually there is no explanation about how to put Javascript in addons in the official docs.

This hook works fine for other addons, but not for mine. There is no output in the console, and actually my code is not included in the page nor the compiled scripts.

I have read about the my_changes addon but since I'm actually creating an addon, it doesn't seems a good idea to me to use that way.

The Javascript code is a simple:

console.log('Got executed!');

and I'm putting that single line in the file:

~\cscart\design\backend\templates\addons\a_test_cscart_addon\hooks\addons\scripts.post.tpl

I have also seen people talking about script.tpl and even scripts_head.tpl or similar, but still don't know how to get my single liner executed without using the my_changes addon.

Thanks for your time and help.

  • I guess that tpl hooks for the addons section of the backend have a hard way to get its work done. Before this issue with JS, I was trying to use the `~/addons/update.[pre/post].tpl` template hook without luck and had to move my logic to `~/addons/manage.pre.tpl` which wasn't the right solution. And now I found this with `~/addons/scripts.post.tpl` issue! – Dayron Armas Jun 25 '19 at 18:37

2 Answers2

1

If you want the js to be executed in the backend, you have to use this path:

~\cscart\design\backend\templates\addons\a_test_cscart_addon\hooks\index\scripts.post.tpl

then, if you want to only execute some code, let's say in the addons.manage area of the site, you just do this in the file:

{if ($runtime.controller == "addons" && $runtime.mode == "manage")}
    console.log('Got executed!');
{/if}

that is the way you control where the code is executed.

Cornel Raiu
  • 2,758
  • 3
  • 22
  • 31
0

Not right path. You want execute on front or backend. For front: in current templates or "responsive" folder create: templates/addons/a_test_cscart_addon/hooks/index/scripts.post.tpl

  • Thank you. As can be seen in the path, it is for `backend`. I guess that my tpl should not be for `index`, I want it only in the `addons` part of cscart. If I use `index` in the path instead of `addons`, I can imagine that it would be loaded everywhere or not loaded at all on the addons management of cscart admin interface. – Dayron Armas Jun 25 '19 at 15:27
  • I tried with `index` and the script gets loaded in every interface of the backend, and it is not my intention to do that. – Dayron Armas Jun 25 '19 at 18:27
  • scripts.post.tpl will only get executed with the `index` in the path. If you want to execute smth for a specific controller just wrap your code in: `{if $runtime.controller="your_controller"}console.log("executed");{/if}` – Cornel Raiu Jul 09 '19 at 23:44