How to move repeating code into an include template 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 %}

  &lt;article class="grid-60"&gt;

    &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;

    {{ entry.body }}

  &lt;/article&gt;&lt;!-- grid-60 --&gt;

{% endblock %}


And for completeness, I have the following in my _layout template:

{% set metaTitle = globalMetadata.metaTitle %}
{% set metaDescription = globalMetadata.metaDescription %}
{% set metaKeywords = globalMetadata.metaKeywords %}

&lt;title&gt;{% if title is defined %}{{ title }}{% else %}{{ globalMetadata.metaTitle }}{% endif %} | {{ siteName }}&lt;/title&gt;
&lt;meta name="description" content="{% if description is defined %}{{ description }}{% else %}{{ globalMetadata.metaDescription }}{% endif %}" /&gt;
&lt;meta name="keywords" content="{% if keywords is defined %}{{ keywords }}{% else %}{{ globalMetadata.metaKeywords }}{% endif %}" /&gt;


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.
That seo code will need to be in a block of its own. 
Create a seo block in your _layout template and then wrap your seo code in your include template with block tags of the same name.