<p>Just put your javascript file in the templates folder. E.g. place a file called <code>external.js</code> in the folder <code>craft/templates/includes/</code>. Then include it like this <code>&lt;script src="/includes/external.js" type="text/javascript"&gt;&lt;/script&gt;</code></p>

<p>I don't know if this is an official way to do it, but it works.</p>

CC BY-SA 3.0
2016-07-07T16:06:58.973Z
This didn't work for me. It says that it doesn't recognize the curly brackets as part of the code.
Jesse Winton
CC BY-SA 3.0
2016-07-07T19:23:55.930Z
Just guessing, but maybe you need to escape certain parts of the javascript code? Have a look at [this](http://twig.sensiolabs.org/doc/templates.html#working-with-automatic-escaping).
Bart Rylant
CC BY-SA 3.0
2016-07-07T09:21:47.317Z
2016-07-07T10:09:37.700Z
2016-07-07T10:09:37.700Z
Bart Rylant
<p>I'm a little bit new to Craft, but it's already my favorite CMS, even after 8 years or so in Wordpress.</p>

<p>Here's what I need to do. I have integrated a preloader into my site, using preload.js, and I need to know if there is a way to utilize Twig within my JavaScript files.</p>

<p>Essentially, preload.js gets all of the assets to preload from a load Manifest, which is created with JSON. I got this working properly as an inline script, simply pulling the sources of all my assets, based on <a href="https://craftcms.stackexchange.com/questions/2006/how-do-i-display-a-list-of-assets-with-the-entries-they-are-related-to">this question.</a> What I need to know is, is there a way to use these Twig tags inside of an external JavaScript file? I know that there is in liquid, but I'd like to get this working here as well.</p>

<p>Thanks.</p>

<templating>
Twig in External jQuery
# Twig in External jQuery

(Asked by: username_0 on 2016-07-07)

I'm a little bit new to Craft, but it's already my favorite CMS, even after 8 years or so in Wordpress.

Here's what I need to do. I have integrated a preloader into my site, using preload.js, and I need to know if there is a way to utilize Twig within my JavaScript files.

Essentially, preload.js gets all of the assets to preload from a load Manifest, which is created with JSON. I got this working properly as an inline script, simply pulling the sources of all my assets, based on [this question.](https://craftcms.stackexchange.com/questions/2006/how-do-i-display-a-list-of-assets-with-the-entries-they-are-related-to) What I need to know is, is there a way to use these Twig tags inside of an external JavaScript file? I know that there is in liquid, but I'd like to get this working here as well.

Thanks.

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2016-07-07. Score: 2

Just put your javascript file in the templates folder. E.g. place a file called `external.js` in the folder `craft/templates/includes/`. Then include it like this

I don't know if this is an official way to do it, but it works.

> **Comments:**
> **username_0**: This didn't work for me. It says that it doesn't recognize the curly brackets as part of the code.  
> **username_1**: Just guessing, but maybe you need to escape certain parts of the javascript code? Have a look at [this](http://twig.sensiolabs.org/doc/templates.html#working-with-automatic-escaping).  

---

