0

I am trying to load some JS from a template into my main layout.twig. I have a block at the bottom of my layout.twig called {% block js %}. It's pulling in some Twig code, so it needs to be in a Twig file, and it relies on jquery so it needs to load after my main site-wide app.js loads. The js block is placed below the app.js reference in my layout.twig, but the js block code is outputting above the app.js when the site loads, resulting in jquery errors since jquery loads after the js block.

I added numbers at the start of each line below to show where I would expect those lines to load in the actual output. The numbers should be in order (1,2,3,4,5) in the output, but they're not in order.

Is there something I can do to force the js block to load in the order displayed in layout.twig?

Template:

1.    {% include 'components/_footer' %}    
2.    <script src="/assets/js/app.js"></script>
3.    {% block js %}{% endblock %}
4.  </body>
5. </html>

Output:

3.    <script></script> from {% block js %}
1.    <footer></footer>
2.    <script src="/assets/js/app.js"></script>
4.  </body>
5. </html>
  • Not able to replicate this... What's in your {% block js %}? Have you tried the {% js %} tag (docs) – Oli May 1 at 13:04

Your Answer

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

Browse other questions tagged or ask your own question.