We include an external script from a vendor and the URL doesn't end in .js. It returns JavaScript that creates a widget in place. I was able to fake it out by adding a '&js=.js' parameter but it still rendered with the other scripts.

Is there an option for {% js %} to render in place? A {% js at right-here %}?

  • 1
    I may be missing something here, but you probably just want to use the <script> tag?
    – dmatthams
    Dec 3 '20 at 9:07
  • I may be too. That's the only thing I've got working so far, but I'm not sure if there's a standard way to do it.
    – ponies
    Dec 3 '20 at 19:12
  • The {% js %} tag can only inject javascript at the end or beginning of the page so it's fine to use the <script> tag if you need something in place.
    – dmatthams
    Dec 4 '20 at 9:33

In your template you can use the <script src=""> tag as it's a combination of the html and twig. So it will load your external javascript files.

Also you can use the inline javascript or javascript file using this tag {% js %} like below.

{# Use for JS file #}
{% js "/assets/js/script.js" %}

{# Use as a JS code block #}
{% js %}

console.log("hello World");    

{% endjs %}
  • Inside a {% js %} block, could you pull in an external script and run it?
    – ponies
    Dec 24 '20 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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