<p>In my example (<code>{% block content %}</code>), the variable is "content" so you can create additional <em>blocks</em> by naming the block something different and outputting it in your layout template - reference <a href="http://twig.sensiolabs.org/doc/tags/extends.html" rel="nofollow">Twig docs for more examples</a>.</p>

<p>By adding a new block <code>{% block meta %}</code> I was able to output entry specific meta data.</p>

<p><strong>Example:</strong></p>

<p>Child template: <code>group/template.html</code></p>

<pre><code>{% extends "_layout" %}

{% block meta %}
&lt;meta property="og:title" content="{{ entry.title }}"&gt;
&lt;meta property="og:site_name" content="{{ siteName }}"&gt;
&lt;meta property="og:type" content="article"&gt;
&lt;meta property="og:locale" content="en_US"&gt; 
&lt;meta property="og:url" content="{{ entry.url }}"&gt;
&lt;meta property="og:description" content="{{ entry.body|hacksaw(chars=300) }}"&gt;
&lt;meta property="article:published_time" content="{{ entry.postDate|date('U') }}"&gt;   
{% endblock %}

{% block content %}
...
{% endblock %}
</code></pre>

<p>Parent template: <code>_layout.html</code></p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    {% block meta %}{% endblock %}
&lt;/head&gt;
&lt;body&gt;
    {% block content %}{% endblock %}
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

CC BY-SA 3.0
2014-07-09T16:46:44.930Z
Also, check out the Twig parent() function: http://twig.sensiolabs.org/doc/functions/parent.html
Andrea DeMers
CC BY-SA 3.0
2014-07-09T19:57:19.387Z
Are you really using the `embed` tag? Not just a simple [`extends '_layout'`](http://twig.sensiolabs.org/doc/tags/extends.html) with [`parent()`](http://twig.sensiolabs.org/doc/functions/parent.html)?
carlcs
CC BY-SA 3.0
2014-07-09T20:19:05.120Z
I updated my answer with both templates' code, in the simplest sense. I could be wrong, but I don't think `parent()` applies here as I'm not trying to append anything to the parent. I think using the object method mentioned in the other answer might be better. Iterating through that and only having one line of markup in the parent template (layout) sounds better to me.
Ryan Shrum
CC BY-SA 3.0
2014-07-09T21:25:57.910Z
@RyanShrum I think Christian meant that you could have a `{% block head %}` inside the `<head>` element that includes the page title, stylesheet references, etc. and then easily append more meta properties (or any valid `<head>` markup) as needed. I've done this as well, I'm just a fan of the object syntax for whatever reason.
Matt Stein
CC BY-SA 3.0
2014-07-09T21:26:41.193Z
@Ryan, sure you don't need to use `parent()` but it is an elegant way to leave shared meta or other head tags in `_layout` and include / override it dynamically.
carlcs
CC BY-SA 3.0
2014-07-09T15:34:23.627Z
2014-07-09T21:53:21.110Z
2014-07-09T21:53:21.110Z
Ryan Shrum
<h2>Option A: Set individual properties in the child template</h2>

<p>If you want to set specific meta properties instead of defining a block, you can include lines like this in <code>_layout.html</code>:</p>

<pre><code>{% if metaDescription is defined %}&lt;meta property="description" content="{{ metaDescription }}"&gt;{% endif %}
{% if metaKeywords is defined %}&lt;meta property="keywords" content="{{ metaKeywords }}"&gt;{% endif %}
</code></pre>

<p>Then, you can simply set either value from within the child template...</p>

<pre><code>{% set metaDescription = entry.summaryFieldOrWhatever %}
{% set metaKeywords = entry.tagField %}
</code></pre>

<h2>Option B: Pack an array with properties in the child template</h2>

<p>...or as you pointed out, that could get cumbersome if you have a whole bunch of meta data. In that case, you could take a similar approach with an object instead of a block. So in <code>_layout.html</code>:</p>

<pre><code>{% if metaProperties is defined %}
     {% for prop in metaProperties %}
     &lt;meta property="{{ prop.name }}" content="{{ prop.value }}"&gt;
     {% endfor %}
{% endif %}
</code></pre>

<p>In the child template, then, you could...</p>

<pre><code>{% set metaProperties = [
    { name: "description", value: entry.summaryFieldorWhatever },
    { name: "keywords", value: entry.tagField }
] %}
</code></pre>

<p>This way, you could set as many items as you'd like and still avoid passing markup to the parent template.</p>

<p>[EDIT] Typo fix</p>

CC BY-SA 3.0
2014-07-09T16:25:46.400Z
This is great for simple strings, but when it's 10 lines of open graph code, I thought a block was better suited.
Ryan Shrum
CC BY-SA 3.0
2014-07-09T16:28:09.317Z
@RyanShrum It very well may be, but you can also pass objects too! I'll update my answer since it'd make an awful comment.
Matt Stein
CC BY-SA 3.0
2014-07-09T17:28:42.983Z
Would love if someone could weigh in here on best practice with specific regard to performance. I love the idea of passing an object. Are there any performance hits using the block method?
Ryan Shrum
CC BY-SA 3.0
2014-07-09T19:47:53.930Z
@Ryan I wouldn't be concerned about performance, with either method. Which method do you prefer in other respects? I'd say the *Block* method is easier to maintain (readability) as you have html and values side by side. Using an *Object* lets you call your variable values in the complete parent template, you don't need to inject your meta data html as one single block.
carlcs
CC BY-SA 3.0
2014-07-09T19:48:05.623Z
@RyanShrum My guess is that the performance difference would be negligible, but my guess is about as authoritative as math tips from a houseplant.
Matt Stein
CC BY-SA 4.0
2014-07-09T16:12:39.847Z
2019-01-20T22:57:58.400Z
2019-01-20T22:57:58.400Z
Matt Stein
<p>I have a main layout file and extending it with another template. I have my markup and content in the <code>{% block content %}{% endblock %}</code> however, I'd like to add content to another "block" on the individual entry page for meta data - that would require adding another <code>{% block content %}</code> to my template as it would need to be output in a different area of the layout template.</p>

<p>Essentially, is there a way to add a second block (perhaps name it differently) or is my best bet to set all that meta data as a variable and pass it that way?</p>

<entries><templating>
How do I add an HTML block from template extension to main layout template? (i.e. meta data)
# How do I add an HTML block from template extension to main layout template? (i.e. meta data)

(Asked by: username_0 on 2014-07-09)

I have a main layout file and extending it with another template. I have my markup and content in the `{% block content %}{% endblock %}` however, I'd like to add content to another "block" on the individual entry page for meta data - that would require adding another `{% block content %}` to my template as it would need to be output in a different area of the layout template.

Essentially, is there a way to add a second block (perhaps name it differently) or is my best bet to set all that meta data as a variable and pass it that way?

**Tags:** entries, templating


---

## 2 answers

### Answer by username_3 on 2014-07-09. Score: 7 | Accepted answer

Option A: Set individual properties in the child template
If you want to set specific meta properties instead of defining a block, you can include lines like this in _layout.html:
{% if metaDescription is defined %}{% endif %}
{% if metaKeywords is defined %}{% endif %}

Then, you can simply set either value from within the child template...
{% set metaDescription = entry.summaryFieldOrWhatever %}
{% set metaKeywords = entry.tagField %}

Option B: Pack an array with properties in the child template
...or as you pointed out, that could get cumbersome if you have a whole bunch of meta data. In that case, you could take a similar approach with an object instead of a block. So in _layout.html:
{% if metaProperties is defined %}
     {% for prop in metaProperties %}
     
     {% endfor %}
{% endif %}

In the child template, then, you could...
{% set metaProperties = [
    { name: "description", value: entry.summaryFieldorWhatever },
    { name: "keywords", value: entry.tagField }
] %}

This way, you could set as many items as you'd like and still avoid passing markup to the parent template.
[EDIT] Typo fix

> **Comments:**
> **username_0**: This is great for simple strings, but when it's 10 lines of open graph code, I thought a block was better suited.  
> **username_3**: @RyanShrum It very well may be, but you can also pass objects too! I'll update my answer since it'd make an awful comment.  
> **username_0**: Would love if someone could weigh in here on best practice with specific regard to performance. I love the idea of passing an object. Are there any performance hits using the block method?  
> **username_2**: @Ryan I wouldn't be concerned about performance, with either method. Which method do you prefer in other respects? I'd say the *Block* method is easier to maintain (readability) as you have html and values side by side. Using an *Object* lets you call your variable values in the complete parent template, you don't need to inject your meta data html as one single block.  
> **username_3**: @RyanShrum My guess is that the performance difference would be negligible, but my guess is about as authoritative as math tips from a houseplant.  

---

### Answer by username_0 on 2014-07-09. Score: 5

In my example ({% block content %}), the variable is "content" so you can create additional blocks by naming the block something different and outputting it in your layout template - reference Twig docs for more examples.
By adding a new block {% block meta %} I was able to output entry specific meta data.
Example:
Child template: group/template.html
{% extends "_layout" %}

{% block meta %}



 


   
{% endblock %}

{% block content %}
...
{% endblock %}

Parent template: _layout.html



    {% block meta %}{% endblock %}


    {% block content %}{% endblock %}

> **Comments:**
> **username_1**: Also, check out the Twig parent() function: http://twig.sensiolabs.org/doc/functions/parent.html  
> **username_2**: Are you really using the `embed` tag? Not just a simple [`extends '_layout'`](http://twig.sensiolabs.org/doc/tags/extends.html) with [`parent()`](http://twig.sensiolabs.org/doc/functions/parent.html)?  
> **username_0**: I updated my answer with both templates' code, in the simplest sense. I could be wrong, but I don't think `parent()` applies here as I'm not trying to append anything to the parent. I think using the object method mentioned in the other answer might be better. Iterating through that and only having one line of markup in the parent template (layout) sounds better to me.  
> **username_3**: @RyanShrum I think Christian meant that you could have a `{% block head %}` inside the `<head>` element that includes the page title, stylesheet references, etc. and then easily append more meta properties (or any valid `<head>` markup) as needed. I've done this as well, I'm just a fan of the object syntax for whatever reason.  
> **username_2**: @Ryan, sure you don't need to use `parent()` but it is an elegant way to leave shared meta or other head tags in `_layout` and include / override it dynamically.  

---

