<p>You could define a variable in you child template:</p>

<pre><code>{% set bodyAttributes = 'onload="load()" onunload="GUnload()"' %}
</code></pre>

<p>And then output the value in your parent _layout.html template. Use the <a href="http://twig.sensiolabs.org/doc/tests/defined.html" rel="nofollow"><code>is defined</code></a> test, so that not every template extending this file has to have that variable defined. I'm using <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow">ternary syntax</a> here, to keep the code nice and lean:</p>

<pre><code>&lt;body {{ bodyAttributes is defined ? bodyAttributes : '' }}&gt;
</code></pre>

<p>As I generally like to have as few logic in my Twig output tags as possible (in particular when they output classes and attributes to already cluttered HTML tags), I would write that code like so:</p>

<pre><code>{% set bodyAttributes = bodyAttributes is defined ? bodyAttributes %}

&lt;body {{ bodyAttributes }}&gt;
</code></pre>

CC BY-SA 3.0
2015-08-26T16:33:02.910Z
2015-08-26T16:33:02.910Z
carlcs
<p>I'll give you my specific use-case here and then see if it might also be more generally applicable in other scenarios: We have a new Craft PRO site running happily, and in our <code>/templates/</code> we have a <code>_layout.html</code> file that every other template then uses and extends. That <code>_layout</code> includes common <code>&lt;head&gt;</code> content, main navigation, footers, etc. Nothing surprising.</p>

<p>But on one sub-page we are displaying Google Maps using various calls to Google's Mapping API. One requirement for the Google Maps, however, is that the  tag trigger Google's JS when the page loads and unloads:</p>

<pre><code>  &lt;body onload="load()" onunload="GUnload()"&gt;
</code></pre>

<p>Currently that <code>&lt;body&gt;</code> tag is in <code>_layout.html</code> so that it can be consumed by the mapping page. But for all other pages it's superfluous, and throws a minor error in the JS console when you inspect the page.</p>

<p>My question is: Is there a way to write a page template so that it <code>_extends</code> another base template but also overwrites just a small portion of it? For instance, could I remove the <code>onload</code> / <code>onunload</code> values from _layout.html and somehow have my mapping page INSERT them back into the <code>&lt;body&gt;</code> tag for just that page?</p>

<p>Or is there a more elegant way to have my mapping page draw upon <code>_layout.html</code> but keep the <code>onload</code> / <code>onunload</code> calls isolated to just that page? Or maybe have _layout.html look for what page is calling it and then return one <code>&lt;body&gt;</code> tag for the mapping and another <code>&lt;body&gt;</code> tag for all other pages?</p>

<p>The Google Maps API makes it pretty clear that this JS function be called at the <code>&lt;body&gt;</code> level, and not just on a <code>&lt;div&gt;</code>.</p>

<templating><extends><google-maps>
Override a portion of an extends template
# Override a portion of an extends template

(Asked by: username_0 on 2015-08-26)

I'll give you my specific use-case here and then see if it might also be more generally applicable in other scenarios: We have a new Craft PRO site running happily, and in our /templates/ we have a _layout.html file that every other template then uses and extends. That _layout includes common  content, main navigation, footers, etc. Nothing surprising.
But on one sub-page we are displaying Google Maps using various calls to Google's Mapping API. One requirement for the Google Maps, however, is that the  tag trigger Google's JS when the page loads and unloads:
  

Currently that  tag is in _layout.html so that it can be consumed by the mapping page. But for all other pages it's superfluous, and throws a minor error in the JS console when you inspect the page.
My question is: Is there a way to write a page template so that it _extends another base template but also overwrites just a small portion of it? For instance, could I remove the onload / onunload values from _layout.html and somehow have my mapping page INSERT them back into the  tag for just that page?
Or is there a more elegant way to have my mapping page draw upon _layout.html but keep the onload / onunload calls isolated to just that page? Or maybe have _layout.html look for what page is calling it and then return one  tag for the mapping and another  tag for all other pages?
The Google Maps API makes it pretty clear that this JS function be called at the  level, and not just on a .

**Tags:** templating, extends, google-maps


---

## 1 answers

### Answer by username_1 on 2015-08-26. Score: 3 | Accepted answer

You could define a variable in you child template:
{% set bodyAttributes = 'onload="load()" onunload="GUnload()"' %}

And then output the value in your parent _layout.html template. Use the is defined test, so that not every template extending this file has to have that variable defined. I'm using ternary syntax here, to keep the code nice and lean:


As I generally like to have as few logic in my Twig output tags as possible (in particular when they output classes and attributes to already cluttered HTML tags), I would write that code like so:
{% set bodyAttributes = bodyAttributes is defined ? bodyAttributes %}


---

