<p>Are you on Craft 3 or 2? In 3, you can use the built-in <code>{% js %} ... {% endjs %}</code> tag to inject scripts into tags named <code>{{ endBody() }}</code> or <code>{{ beginBody() }}</code> that you place wherever you like in your layout.</p>

<p>When outputting from the entry, also be sure to use the <code>|raw</code> filter.</p>

<p><a href="https://docs.craftcms.com/v3/dev/tags/js.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/tags/js.html</a></p>

<p>Craft 2 had a similar function: <a href="https://docs.craftcms.com/v2/templating/includejs.html" rel="nofollow noreferrer">https://docs.craftcms.com/v2/templating/includejs.html</a></p>

<p>...Or you can just do it all manually with a Twig block - one thing that's a bit annoying about Craft's built-in tags is that they automatically include the <code>&lt;script&gt;</code> tags, which in turn means you won't get JS syntax highlighting in your editor. To do it manually I'd do this:</p>

<pre><code>=========================================
_layout.twig:
=========================================

&lt;html&gt;
&lt;head&gt;
    {{ block('extraHeadData') }}
&lt;/head&gt;
&lt;body&gt;
    {{ block('content') }}  
    {{ block('extraFootData') }}
&lt;/body&gt;
&lt;/html&gt;

=========================================
template.twig:
=========================================

{% extends '_layout' %}

{% block 'content' %}&lt;h1&gt;Hi&lt;/h1&gt;{% endblock %}    

{% block extraHeadData %}
    &lt;script&gt;console.log('extra stuff in the head if you really need it');&lt;/script&gt;
{% endblock %}

{% block extraFootData %}
    &lt;script&gt;alert('hi');&lt;/script&gt;
    {{ entry.scripts ? entry.scripts|raw }}
{% endblock %}
</code></pre>

<p>Edit to add: I forgot to mention the one big advantage of using Craft's built-in methods over native Twig blocks is that they can work anywhere in the template regardless of the nesting level or scope of your blocks. For this reason it's usually preferable. (Even if you start with a simple inheritance model where each template <code>@extend</code>s the layout, when the site grows and you start using shared intermediary templates to keep the code DRY, you can start running into very fiddly scope issues).</p>

CC BY-SA 4.0
2018-08-01T17:29:36.713Z
I'm on craft 3. Great! thank you so much for the info!
Danielle Frappier
CC BY-SA 4.0
2018-08-01T21:17:45.187Z
the {% includeJs %} tag does the same thing in Craft 2
foamcow
CC BY-SA 4.0
2018-08-01T16:32:54.127Z
2018-08-02T08:46:58.840Z
2018-08-02T08:46:58.840Z
James Smith
<p>I have a field call "scripts" to house scripts like a marketo form script. This gets added in an entry template, but I need to pass this to the layout (if there is something in the field) and render in the footer. </p>

<p>Is this possible? I'm open to ideas or suggestions. </p>

<layout>
pass data to _layout?
# pass data to _layout?

(Asked by: username_0 on 2018-08-01)

I have a field call "scripts" to house scripts like a marketo form script. This gets added in an entry template, but I need to pass this to the layout (if there is something in the field) and render in the footer.

Is this possible? I'm open to ideas or suggestions.

**Tags:** layout

---

## 1 answers

### Answer by username_1 on 2018-08-01. Score: 2 | Accepted answer

Are you on Craft 3 or 2? In 3, you can use the built-in `{% js %} ... {% endjs %}` tag to inject scripts into tags named `{{ endBody() }}` or `{{ beginBody() }}` that you place wherever you like in your layout.

When outputting from the entry, also be sure to use the `|raw` filter.

<https://docs.craftcms.com/v3/dev/tags/js.html>

Craft 2 had a similar function: <https://docs.craftcms.com/v2/templating/includejs.html>

...Or you can just do it all manually with a Twig block - one thing that's a bit annoying about Craft's built-in tags is that they automatically include the `{% endblock %}
{% block extraFootData %}
{{ entry.scripts ? entry.scripts|raw }}
{% endblock %}`

Edit to add: I forgot to mention the one big advantage of using Craft's built-in methods over native Twig blocks is that they can work anywhere in the template regardless of the nesting level or scope of your blocks. For this reason it's usually preferable. (Even if you start with a simple inheritance model where each template `@extend`s the layout, when the site grows and you start using shared intermediary templates to keep the code DRY, you can start running into very fiddly scope issues).

> **Comments:**
> **username_0**: I'm on craft 3. Great! thank you so much for the info!  
> **username_2**: the {% includeJs %} tag does the same thing in Craft 2  

---

