1

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.

Thanks.

Jesse Winton
  • 359
  • 1
  • 9

1 Answers1

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

carlcs
  • 36,220
  • 5
  • 62
  • 139
Bart Rylant
  • 317
  • 1
  • 4