1

Might be basic, but not sure where I'm doing wrong. I was trying to display the post date of an article using this code

{{ entry.postDate|date('medium') }}.

But getting the error stating "Variable "entry" does not exist."

2

The entry variable is automatically created for the individual entry pages that you define in your section settings.

For example, if your Section has the following Site Settings:

  • URI Format: blog/{slug}
  • Template: blog/_entry

You can expect an entry variable to exist when the blog/_entry is loaded.

blog/_entry.twig

{{ entry.postDate|date('medium') }}

some-other-template.twig

If you are trying to access an entry in some other template, you would need to make sure you define the entry variable on the page.

{% set entry = craft.entries.id(123).one() %}
{{ entry.postDate|date('medium') }}

You don't need to query the entry by ID, it was just an easy example. You can use any element query options and read more about them in the Querying Entries section of the docs.

is defined

If you have a scenario where you don't know if the entry will exist or not, you can check for its existence using is defined:

{% if entry is defined %}
  {{ entry.postDate|date('medium') }}
{% endif %} 

debugging

If you are curious what does exist, you can turn on the devMode setting in your config/general.php settings and dump the variable to see more about what the application is seeing:

{{ dump(entry) }}

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.