babycakes
babycakes

Reputation: 546

How do I include a custom JS (JQuery) file in a .tpl?

So I have an HTML page that I want to take apart and put into my website in a more developer friendly manner. I was using Iframes but now I decided to integrate it into my .tpl file. For the life of me I can't figure out how to include a custom js file in the .tpl The way other files are included is in a php file but this is just to include the config file. I managed to put the HTML in the .tpl fine, the last part I need is the javascript. I believe the issue is that the JS is uses Jquery so I need to include that library but I can't seem to make it work. Any ideas? I've used literal and that didn't work either....

I can provide code examples on reqeust

Upvotes: 1

Views: 3876

Answers (3)

babycakes
babycakes

Reputation: 546

So i managed to fix it, I used in the template file and in the php file I include the config and then under that $(document).ready(function(){});

Upvotes: 1

Battle_707
Battle_707

Reputation: 708

Depending on your template engine, you can either directly edit in the code directly into your template file or you can put in a reference point that your template engine can then pick up on.

If the scripts needs to run on all of the pages when that template file is run, you can just put in the javascript code directly into the template file. There are some major unknowns for me here, so you should always check back with your template engine's support; for example, if the code needs to run in the header, your engine likely has a dedicated way to inject the code. The advantage of putting the code directly into the template file is that you will not waste any additional resources 'templating it in', if you will, every time.

If you only want to run the script on specific pages, but more than just those pages uses that template file, you'll need to add an anchor to the template file and then edit your PHP script to place it in/ ignore the anchor according to your needs. Anchors, surprise, surprise, vary strongly with the different template engines. Smarty will, as Darien pointed out allready, accept anchors that are delimited with curly brackets, but that might not work in your case. If the template file is loaded and the page should not use the script, make sure to remove the anchor in your php script. So, your PHP should look similar to this:

if ($onRightPage) {

    // Call engine function to add in the script

} else {

    // Call engine function to remove the anchor

}

Upvotes: 0

Darien
Darien

Reputation: 3592

I suspect when you say "tpl" you mean a Smarty template. (Which is a more popular tag than "tpl")

Ideally, your existing template system should be set up so that there is either some convention (ex. {capture}) or some other facility (like a plugin) that lets you to "retroactively" place <script type="text/javascript" src="xxx.js"></script> into the <head> of the document.

However, you can also put the <script></script> tags into the body of the page at an appropriate spot.

If you are trying to directly enter javascript directly, you will almost certainly require Smarty {literal} protection, but that is only because otherwise Smarty will explode trying to handle all of Javascript's { and } if they are directly inside the tpl file.

Upvotes: 1

Related Questions