Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-17T11:19:05.620Z
Answers.LastActivityDate: 2018-04-17T11:19:05.620Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-17T10:34:58.400Z
Comments.Text: 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 %}`
Comments.UserDisplayName: KSPR
Tags: <tags><seo><layout><archives>
Title: SEO titles/descriptions for tags and archive pages
ThreadText: # 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 %}
  
  <title>{{ siteName }} - {% if seoTitle is not empty %}{{ seoTitle }}{% else %}{{ title }}{% endif %}</title>
  <meta name="description" content="{% if seoDescription is defined %}{{ seoDescription }}{% else %}{{ title }}{% endif %}"/>
  {% if url is defined %}<link rel="home" href="{{ url }}" />{% endif %}
  <meta name="twitter:card" content="summary" />
  {% if url is defined %}<meta property="og:url" content="{{ url }}" />{% endif %}
  <meta property="og:title" content="{% if seoTitle is not empty %}{{ seoTitle }}{% else %}{{ title }}{% endif %}" />
  <meta property="og:description" content="{% if seoDescription is defined %}{{ seoDescription }}{% else %}{{ title }}{% endif %}" />
  {% if seoImage is defined %}
      {% for image in seoImage %}
          <meta property="og:image" content="{{ image.getUrl()}}" />
      {% 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 %}
  <title>{{ siteName }} - {{ seoTitle }}</title>
  
  ```
  
  ### \_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
  
  
  ---
  
  