How to add script to head tag for certain pages only

I want to add or include the following in the head tag:


```php


```



I know how to add this so it's included on every page, but I want it in the header of only one page.



I've done this before when developing a site that uses a different CMS package that utilizes Smarty.



What I did then, was to add {$smarty.capture.header} just before the closing head tag in the global header template file. Then when I wanted something added to the head tag, for a specific page, I would add something like…

{capture name=“header”}{/capture}

… to the top of that pages template.



I can't get this to work for CS-Cart 3.x



To be more specific, I want to add a certain JavaScript source to the head tag of the checkout page.



Is this possible without adding it to every page?



I tried adding the following just above the closing head tag of 'skins\basic\customer\index.tpl' and I also tried adding it to the bottom of 'skins\basic\customer\common_templates\scripts.tpl'


{$smarty.capture.header}




And the following to 'skins\basic\customer\views\checkout\checkout.tpl'


{capture name="header"}{/capture}

Hi

You need to use the hooks system.



There is a hook for the checkout page so you can put in my_changes folder of your skin in addons folder, a new folder called checkout and then create a file scripts.pre.tpl with this script inside.



To be honest I haven't tried it in v3 but this is how it's done in v2 so it must be similar.



Have a look also in this article CS-Cart Documentation — CS-Cart 4.15.x documentation



Fotis

Fotis, thanks for the tip but that does not work. I don’t think the hooks work that way.



If I want to add a file named ‘scripts.pre.tpl’, it has to be added to the ‘\skins\basic\customer\addons\my_changes\hooks\index’ folder but that adds the contents of ‘scripts.pre.tpl’ to every page because ‘script.tpl’ loads in every page.



Unless I’m too tired and blurry eyed, I think that article you mentioned verifies what I’m saying.



Thanks,



Jeff :grin:

Put your info in your scripts.post.tpl file inside:


{if $controller == 'checkout' }
Your script code here
{/if}


This will include your changes on all pages related to 'checkout'. If you want to be specific on the MODE or the STEP, then you'll have to add additional conditions to the if statement.

tbirnseth, that’s exactly what I was looking for.



Thank you :grin:



I read about controllers (http://docs.cs-cart…ocs-controllers) but there’s not a lot of info on them.



Can you give me a code example if I wanted to make this checkout step number three specific?



Is there a list of all of the available controllers, steps and modes variables?

[quote name='jeffshead' timestamp='1342965384' post='141238']

I read about controllers (http://docs.cs-cart…ocs-controllers) but there's not a lot of info on them.

[/quote]



Hi jeffshead,

[center],[/center]

I'm wondering if you found a solution to insert the script into a single page?

I'm having difficulties to do this as well. Will appreciate your help!

Wrap it in something like:

```php

{if $controller == 'checkout' and $mode == 'summary'}

{literal}


{/literal}

{/if}

```

If you need it for a particular 'page' (in cs-cart vernacular), then your if condition would look more like:


{if $controller == 'pages' && $mode== 'view' && $smarty.request.page_id == 123}


Where '123' is the page ID of the page you are interested in.

Hello,

I want to add a script into the head only for order.complete page (not checkout pages)

How can I do that I tried in scripts.tpl :

{if $controller == 'checkout' and $mode == 'complete'}

....

{/if}

Do you mean hooks/index/scripts.post.tpl in some addon like my_changes? You should probably be using $runtime.controller and $runtime.mode. But you can verify by using:


And then searching the page source (or use browser inspector) for 'mytag:' and seeing what you get.

It would probably be easiest to go to: Design->Layouts->Order landing page->Click gear icon and add to the Custom HTML code area.

I have this at design/template/backend/addons.... x.post.tpl

now it shows correctly in my admin but the script goes to all admin pages.

Is there any way to add it only at specific controller? for example: order_management.add

I tried to add

{if $addons.XX.XX && $addons.admin.TT== 'Y' && $controller == 'order_management' && $mode == 'add'}

but is not working the code is not showing anywhere after i add it

{if $addons.XX.XX && $addons.YYY.TT== 'Y' }
    
    
    {script src=...no-defer=true}
{/if}

Use

{$runtime.controller}

and

{$runtime.mode}

Works! you are great!

Thank you!

Use

{$runtime.controller}

and

{$runtime.mode}