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. 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.


1 Answer 1


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 <script src="/includes/external.js" type="text/javascript"></script>

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

  • This didn't work for me. It says that it doesn't recognize the curly brackets as part of the code. Jul 7, 2016 at 16:06
  • Just guessing, but maybe you need to escape certain parts of the javascript code? Have a look at this. Jul 7, 2016 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.