<p>The better approach in my opinion is to declare the variables in the corresponding templates rather than in the <code>_layout</code> template</p>

<h3>Layout.twig</h3>

<pre><code>{% if seoTitle is not defined or seoTitle is empty %}
    {% set seoTitle = config.seoTitle %} &lt;-- fallback if nothing is defined
{% endif %}
&lt;title&gt;{{ siteName }} - {{ seoTitle }}&lt;/title&gt;
</code></pre>

<h3>_entry.twig</h3>

<pre><code>{% extends 'layout' %}
{% set seoTitle = entry.seoTitle %}
</code></pre>

<h3>_category.twig</h3>

<pre><code>{% extends 'layout' %}
{% set seoTitle = category.seoTitle %}
</code></pre>

<h3>_someOtherTemplates.twig</h3>

<pre><code>{% extends 'layout' %}
{% set seoTitle = 'Some hard coded strings....' %}
</code></pre>

<p>and so on. That way you can define <code>seoTags</code> based on custom properties as well otherwise it would be difficult to determinate tags on dynamic template routes without an entry or a category</p>

CC BY-SA 3.0
2018-04-17T11:19:05.620Z
2018-04-17T11:19:05.620Z
Robin Schambach
<p>I have a blog setup with a Categories, Archive and Tags.</p>

<p>In my _layout template I've been using the following to pull in SEO titles from fields, or Page/Category Titles is that's not been defined.</p>

<pre><code>{% if entry is defined %}
    {% set seoTitle = entry.seoTitle %}
    {% set seoDescription = entry.seoDescription %}
    {% set seoImage = entry.seoImage %}
    {% set seoType = entry.seoType %}
    {% set title = entry.title %}
    {% set url = entry.url %}
{% endif %}


{% if category is defined %}
    {% set seoTitle = category.title %}
    {% set seoDescription = category.title %}
    {% set seoImage = category.seoImage %}
    {% set seoType = category.seoType %}
    {% set title = category.title %}
    {% set url = category.url %}
{% endif %}

&lt;title&gt;{{ siteName }} - {% if seoTitle is not empty %}{{ seoTitle }}{% else %}{{ title }}{% endif %}&lt;/title&gt;
&lt;meta name="description" content="{% if seoDescription is defined %}{{ seoDescription }}{% else %}{{ title }}{% endif %}"/&gt;
{% if url is defined %}&lt;link rel="home" href="{{ url }}" /&gt;{% endif %}
&lt;meta name="twitter:card" content="summary" /&gt;
{% if url is defined %}&lt;meta property="og:url" content="{{ url }}" /&gt;{% endif %}
&lt;meta property="og:title" content="{% if seoTitle is not empty %}{{ seoTitle }}{% else %}{{ title }}{% endif %}" /&gt;
&lt;meta property="og:description" content="{% if seoDescription is defined %}{{ seoDescription }}{% else %}{{ title }}{% endif %}" /&gt;
{% if seoImage is defined %}
    {% for image in seoImage %}
        &lt;meta property="og:image" content="{{ image.getUrl()}}" /&gt;
    {% endfor %}
{% endif %}
</code></pre>

<p>How should I approach this for the archive and tag pages as simply doing something like the below doesn't work?</p>

<pre><code>{% if tag is defined %}
{% set seoTitle = tag.title %}
....
{% endif %}
</code></pre>

CC BY-SA 3.0
2018-04-17T10:34:58.400Z
You could write a conditional based on the url segment if you have the tag or category visible in the url. Something like: `{% if craft.request.getSegment(x) == tag.slug %}`
KSPR
<tags><seo><layout><archives>
SEO titles/descriptions for tags and archive pages
# SEO titles/descriptions for tags and archive pages

(Asked by: username_0 on 2018-04-17)

I have a blog setup with a Categories, Archive and Tags.
In my _layout template I've been using the following to pull in SEO titles from fields, or Page/Category Titles is that's not been defined.
{% if entry is defined %}
    {% set seoTitle = entry.seoTitle %}
    {% set seoDescription = entry.seoDescription %}
    {% set seoImage = entry.seoImage %}
    {% set seoType = entry.seoType %}
    {% set title = entry.title %}
    {% set url = entry.url %}
{% endif %}


{% if category is defined %}
    {% set seoTitle = category.title %}
    {% set seoDescription = category.title %}
    {% set seoImage = category.seoImage %}
    {% set seoType = category.seoType %}
    {% set title = category.title %}
    {% set url = category.url %}
{% endif %}

{{ siteName }} - {% if seoTitle is not empty %}{{ seoTitle }}{% else %}{{ title }}{% endif %}

{% if url is defined %}{% endif %}

{% if url is defined %}{% endif %}


{% if seoImage is defined %}
    {% for image in seoImage %}
        
    {% endfor %}
{% endif %}

How should I approach this for the archive and tag pages as simply doing something like the below doesn't work?
{% if tag is defined %}
{% set seoTitle = tag.title %}
....
{% endif %}

**Tags:** tags, seo, layout, archives
> **Comments:**
> **username_1**: You could write a conditional based on the url segment if you have the tag or category visible in the url. Something like: `{% if craft.request.getSegment(x) == tag.slug %}`  

---

## 1 answers

### Answer by username_2 on 2018-04-17. Score: 5 | Accepted answer

The better approach in my opinion is to declare the variables in the corresponding templates rather than in the _layout template
Layout.twig
{% if seoTitle is not defined or seoTitle is empty %}
    {% set seoTitle = config.seoTitle %} <-- fallback if nothing is defined
{% endif %}
{{ siteName }} - {{ seoTitle }}

_entry.twig
{% extends 'layout' %}
{% set seoTitle = entry.seoTitle %}

_category.twig
{% extends 'layout' %}
{% set seoTitle = category.seoTitle %}

_someOtherTemplates.twig
{% extends 'layout' %}
{% set seoTitle = 'Some hard coded strings....' %}

and so on. That way you can define seoTags based on custom properties as well otherwise it would be difficult to determinate tags on dynamic template routes without an entry or a category


---

