I'm new to Craft and have created some SEO fields similar to this, and the template code appears in all templates as shown below. I don't want to be repeating all that code in every template though, so I thought I'd be better of putting it in an include template (assuming that's how best to handle this in Craft).
But I'm getting an error when I try to do this, so can anyone show me the correct way of doing this please?
Here's my starting code, where I'm repeating the SEO code in every template (see the start and end comments):
{% extends "_layout" %}
{# START OF SEO CODE #}
{% if entry.metaTitle != '' %}
{% set title = entry.metaTitle %}
{% else %}
{% set title = entry.title %}
{% endif %}
{% if entry.metaDescription != '' %}
{% set description = entry.metaDescription %}
{% endif %}
{% if entry.metaKeywords != '' %}
{% set keywords = entry.metaKeywords %}
{% endif %}
{# END OF SEO CODE #}
{% block content %}
<article class="grid-60">
<h1>{{ entry.title }}</h1>
{{ entry.body }}
</article><!-- grid-60 -->
{% endblock %}
And for completeness, I have the following in my _layout template:
{% set metaTitle = globalMetadata.metaTitle %}
{% set metaDescription = globalMetadata.metaDescription %}
{% set metaKeywords = globalMetadata.metaKeywords %}
<title>{% if title is defined %}{{ title }}{% else %}{{ globalMetadata.metaTitle }}{% endif %} | {{ siteName }}</title>
<meta name="description" content="{% if description is defined %}{{ description }}{% else %}{{ globalMetadata.metaDescription }}{% endif %}" />
<meta name="keywords" content="{% if keywords is defined %}{{ keywords }}{% else %}{{ globalMetadata.metaKeywords }}{% endif %}" />
So I've tried moving the SEO code out of the entry template, and into the following include template: _includes/seo_meta:
{# START OF SEO CODE #}
{% if entry.metaTitle != '' %}
{% set title = entry.metaTitle %}
{% else %}
{% set title = entry.title %}
{% endif %}
{% if entry.metaDescription != '' %}
{% set description = entry.metaDescription %}
{% endif %}
{% if entry.metaKeywords != '' %}
{% set keywords = entry.metaKeywords %}
{% endif %}
{# END OF SEO CODE #}
And am trying to call it from my entry template like this:
{% include "_includes/seo_meta" %}
But I'm getting the following error message, and the error is highlighting my include line:
A template that extends another one cannot have a body.
Thanks in advance for any help, Stephen.