1

I am trying to create a meta include to display all my meta data and I have the following set up so far:

3. {% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" %}
  4. <title>{% if entry.metaTitle is defined %}{{ entry.metaTitle }}{% else %}{{ entry.title }}{% endif %} | {{ siteName }}</title>
  5. {% if entry.metaDescription is defined %}
  6. <meta name="description" content="{{ entry.metaDescription }}">
  7. {% endif %}
8. {% endif %}

{# 404 =============== #}

{% if craft.request.getSegment(1)  == "404" %}
  <title>Page Missing | {{ siteName }}</title>
  <meta name="description" content="Sorry the page you are looking for is missing, it may have been moved or deleted, please use the navigation to search for similar content.">
{% endif %}

If I visit main pages the code works and if I go to example.com/404 the code works, but if I visit an invalid url example.com/foo then I get a Twig Error Runtime error:

enter image description here

3

Craft doesn't redirect to 404 if a page isn't found, it just loads the 404 template and keeps the invalid uri.

What currently happens:

  • example.com/invalid_uri
  • Craft doesn't find it and shows the 404 template
  • Your conditional checks if 404 is in the uri, but it isn't.
  • Your code now tries to access the entry variable which isn't defined.
  • This results in a Twig error.

.

Solution:

Add an entry is defined to your conditional:

{% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" and entry is defined %}
....
{% endif %}
| improve this answer | |
  • That did it Victor thank you, for info I also needed to add or entry is not defined to the last 404 conditional which makes sense with what you have explained. – Gareth Jun 24 '14 at 20:56
1

Take a look at what you have in your _includes/_page_meta file at line 4.

Most likely you just need to wrap it in a conditional with is defined like

{% if entry is defined %}
    {# your template stuff here #}
{% endif %}
| improve this answer | |
  • I think Victor's answer is better! – Alex Kendrick Jun 24 '14 at 20:52
  • Thanks Alex, yes Victor's solution fixed it (I added the line numbers in to try and help to). – Gareth Jun 24 '14 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.