The Craft entry‘ variable

Craft makes it extremely easy for us to interact with the content in our database within our templates. Perhaps the most useful item for this is the Craft entry variable.

If Craft knows that the Entries in one of our sections have their own URLs, Craft provides us a variable to help us output all of our content for that Entry. This is true for Singles as well as Channels and Structures where we have selected the setting 'Entries in this section have their own URLs'. Let's take a look at each:

Singles

When we create a Single in our Section settings, we have to tell that Single our Entry Template. The Entry Template maps to the template we want to use to display our content in our templates folder when the Entry URL is visited in the browser.

For example, a user who visits http://website.com/about may be served the content that is displayed in our file craft/templates/about.html

In our about.html template, we can use the Craft entry variable to access all of the content from our About Entry.

{# Single Template #}
{{ entry.title }}
{{ entry.url }}
{{ entry.anyFieldNameUsedInOurAboutSection }}

Channels

While a Channel may have many entries, if we select the setting 'Entries in this section have their own URLs' we can tell Craft which template we want to use for each individual Entry in our Channel.

For example, a user who visits http://website.com/articles/article-slug may be served the content that is displayed in our file craft/templates/articles/_entry.html.

In our articles/_entry.html file we can use the Craft entry variable to access all of the content from our Article Entry for that URL being requested by the browser.

Just as before:

{# Individual Entry Template for our Channel #}
{{ entry.title }}
{{ entry.url }}
{{ entry.anyFieldNameUsedInOurArticleEntry }}

Structures

Individual entries for Structures behave just as Singles and Channels. One difference is that Structures may have an extra segments in their URL referencing their parent Entries. In our Section settings, we still tell a Structure section the Entry Template in which to use to display our individual entry pages, and Craft provides us the entry variable just as before.

{# Individual Entry Template for our Structure  #}
{{ entry.title }}
{{ entry.url }}
{{ entry.anyFieldNameUsedInOurStructureEntry }}

Categories

The behavior we are seeing above also can be applied to other Elements in Craft. When we setup a Category Group in Craft we can select a familiar option as we have seen above: Categories in this group have their own URLs.

We can define our Category URL Format and Category Template and when an individual category is requested from our Category Template, Craft provides us a category variable that behaves just like the entry variable giving us easy access to all of the fields that are available for our Category.

{# Category template #}
{{ category.name }}
{{ category.url }}
{{ category.anyFieldNameUsedInOurCategoryGroupEntry }}

Don't miss any Craft tips, tricks, and community updates