I am using this code to include a js file, but it adds to the <body>:

{% includeJsFile '/path/script.min.js' %}

I need to add it to the <head> instead, how can I do?

2 Answers 2


You can use the getFootHtml function to tell Craft where to output HTML queued up with includeJsFile and includeJs. You'd normally have that tag somewhere at the end of your <body>. But all it does is to get all queued up HTML and output it, so if your JS are already "included", you can use it in <head>.

Another thing you could do is to not use the includeJsFile function to add the HTML to the <head>, but use a different approach. Here's an example using a var and an example using a dedicated Twig block:


{% extends 'base.html' %}

{% set headHtml = '<script src="{{ url('assets/javascripts/myscript.js') }}"></script>' %}

{% block head %}
    {{ parent() }}

    <!-- Scripts -->
    <script src="{{ url('assets/javascripts/myscript.js') }}"></script>
{% endblock %}


    {% block head %}
        <!-- Document head -->
        <meta charset="utf-8">

        {% if headHtml is defined %}
            <!-- Scripts -->
            {{ headHtml }}
        {% endif %}
    {% endblock %}

You can queue files with includeJsFile, includeJs and output getFootHtml() anywhere on your page.

Once you've called getFootHtml() the queue will be cleared allowing you to use includeJsFile again later in your template i.e. both in the head and body.

This doesn't appear in the documentation.

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.