1

I'm a newbie with Craft CMS and just setup my templates. I got multiple .twig files for six pages. If I load my homepage "from scratch" my entries are shown as I want them to. But if I click on a link from another page back to my homepage, the entries are not loaded anymore and I get an error.

Twig Runtime Error – Twig\Error\RuntimeError Variable "entry" does not exist.

How do I fix this? I don't know if you would need more information to help. Just let me know. Thanks for the help in advance! And sorry for being a newb.

1 Answer 1

1

The Twig Runtime Error – Twig\Error\RuntimeError Variable "entry" does not exist. typically happens when the variable has not been declared on the twig file getting pulled.

Its hard to suggest a solution without screenshots or code examples but I think if you check you have declared your entry variable in the homepage.twig file it should resolve.

Another reason why this error occurs is if the first instance of the variable is declared within an if or for statement and called again after said statement. The error happens during this time when the if/for is false and doesn't run, therefor the file never sees the variable created. If this is the case I suggest creating an empty set entry = "" before your if/for statement.

Below are some very high-level examples of what would cause this error and what would not.


Example that would cause error:

//head.twig
{% set entry = value %}

//homepage.twig
{% extends "head.twig" %}
{{ entry }}

Example that would cause error:

//homepage.twig
{% if x = y %}
   {% set entry = value %}
{% endif %}

{{ entry }}

Example that would not cause error:

//homepage.twig
{% set entry = '' %}

{% if x = y %}
   {% set entry = value %}
{% endif %}

{{ entry }}

If you fixed this already I am curious to know what you did. Hopefully my assumption is found helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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