brackets-curlyTemplate Data

This guide explains how to create and use your CMS content withing Elements using Twig, a powerful and user-friendly template language.

The Elements CMS gives you full control over how your content is displayed by supporting the Twigarrow-up-right templating language; a fast, secure, and flexible engine widely used in modern content systems.

Twig is a template engine that lets you create dynamic, reusable templates for your website content. Think of it as a way to create a "skeleton" for your content that automatically fills in the details from your CMS items.

Using CMS Data in Elements Components

You can access your CMS data in any Elements component, as long as that component is placed inside a Collection or Item component. This allows you to create dynamic, data-driven layouts without writing custom templates.

Accessing Your Content Data

Basic Properties

You can access any property from your content files using the {{item.PROPERTY_NAME}} syntax, for example:

  • {{item.title}} - The title of your content

  • {{item.body}} - The main content/body text

  • {{item.date}} - The publication date

  • {{item.tags}} - Any tags you've assigned

  • {{item.excerpt}} - A short summary or excerpt

Custom Properties

If you've added custom properties in your content's frontmatter (the section at the top of your markdown files), you can access them the same way:

In your template, you can use:

  • {{item.category}}

  • {{item.featured}}

  • {{item.custom_field}}

Collection Properties

You can access {{collection.rssURL}} inside the Collection component, which means you can add a Text component to the "Before" or "After" dropzones and link text/svg/anything to the RSS feed for that collection.

Nested Properties

Some properties like an image, contain multiple pieces of information. You can access these using dot notation:

  • {{item.image.url}} - Image URL

  • {{item.image.alt}} - Image alt text

The system can automatically connect related data between different content folders. This is especially useful for tags and authors.

How It Works

If you have a folder structure like this:

When a post references tags or an author, the system automatically connects to the corresponding tag and author files.

Example Content Structure

Post file (posts/my-first-post.md):

Author file (authors/john-doe.md):

Tag file (tags/technology.md):

Accessing Related Data

Once the relationships are established, you can access the full data from the related files:

Author Data:

  • {{item.author.name}} - Full name from author file

  • {{item.author.email}} - Email from author file

  • {{item.author.bio}} - Biography from author file

  • {{item.author.avatar}} - Profile picture from author file

  • {{item.author.website}} - Website from author file

Tag Data:

  • {{item.tags[0].name}} - First tag's full name

  • {{item.tags[0].description}} - First tag's description

Displaying Author Information:

Displaying Tags with Full Data:

Note: if you're using twig syntax directly in an Elements component template, you'll need to wrap the code in an @raw() directive.

Working with URLs

Item URLs

Each piece of content automatically gets a URL. You can access it using:

  • {{item.url}} - The full URL to this content item

  • {{item.slug}} - The URL-friendly version of the title

Pretty URLs

Elements CMS system supports "pretty URLs" which are more readable and SEO-friendly. Instead of URLs like:

You get clean URLs like:

The system automatically generates these pretty URLs based on your component settings, and requires an htaccess file. Read more about setting up Pretty URLs.

Conditional Content

You can show or hide content based on certain conditions:

Looping Through Lists

If you have multiple items (like tags, categories, or related content), you can loop through them:

Formatting Dates

You can format dates in various ways:

Common Template Patterns

Blog Post Template

Product Template

Tips and Best Practices

  1. Always check if properties exist before using them to avoid errors

  2. Use meaningful property names in your content frontmatter

  3. Keep templates simple - focus on structure and let the content drive the design

  4. Test your templates with different types of content to ensure they work properly

  5. Use consistent naming for your properties across all your content

Common Properties Reference

Here's a quick reference of commonly available properties:

Property
Description
Example

item.title

Content title

"My Blog Post"

item.body

Main content

HTML content

item.date

Publication date

2024-01-15

item.author

Author name

"John Doe"

item.slug

URL-friendly title

"my-blog-post"

item.url

Full URL

"/blog/my-blog-post"

item.image

Featured image

"/images/post.jpg"

item.excerpt

Short summary

"This is a brief description..."

item.tags

Content tags

["technology", "web-design"]

item.category

Content category

"Technology"

Remember, you can always add custom properties to your content's frontmatter and access them using the same {{item.property_name}} syntax!

Last updated

Was this helpful?