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."
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:
blog/{slug}blog/_entryYou can expect an entry variable to exist when the blog/_entry is loaded.
{{ entry.postDate|date('medium') }}
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.
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 %}
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) }}