2

I need to run a jQuery file in a twig file in order to bind aqua lift segments. but jQuery is not loaded yet . How can I load jQuery at this point without loading it twice? thanks

2 Answers 2

5

I think your question is that you want to put inline javascript in twig templates, but it does not work, because jquery is not loaded yet?

You can override the default that the libraries are loaded in the footer and put jquery and any of your own js in the header:

mytheme.libraries.yml:

mylibrary:
  version: 1.x
  header: true
  js:
    js/myjavascript.js: {}
  dependencies:
    - core/jquery

mytheme.info.yml:

libraries:
  - mytheme/mylibrary

The important part is header: true.

If you make a dependency on core/jquery for the header, jquery is not loaded a second time in the footer, even if there are multiple dependencies for core/jquery by other libraries.

Now you can put your jquery dependend js code in the header (in this example in myjavascript.js) or put inline js in twig templates.

3
{{ attach_library('core/jquery') }}

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.