Use them in templates

Content types are like models. Let's say you want to display and manage events on your site, each event having a title, a description and a date.

Declare the "events" content type

  cd ~/workspace/my_first_site
  bundle exec wagon generate content_type events \
  title:string description:text event_date:date

2. List events on a page

Content type entries can be accessed with contents.<<content_type slug>>. Open app/views/index.liquid and add in the 'main' block:

  <ul>
  {% for event in contents.events %}
    <li>{{ event.title }}</li>
  {% endfor %}
  </ul>

Test your code http://0.0.0.0:3333. You should see the event list.

3. Add an "event page"

You want to display details about each event on a seperate page. Url will be /events/<<event slug>>. We will use an event template page.

  mkdir app/views/pages/events
  mate app/views/pages/events/content_type_template.liquid

Note that the page filename should be content_type_template.

Add this code (we assume your index page contains a 'main' block):

  ---
  title: Event template page
  content_type: events
  ---
  {% extends 'index' %}
  {% block main %}
  <h1>{{ event.title }} on {{ event.event_date | localized_date: '%m/%d/%Y' }}</h1>
  <p>{{ event.description }}</p>
  {% endblock %}

OK, now let's change the index page to add a link on each event entry:

  <ul>
  {% for event in contents.events %}
    <li><a href="/events/{{ event._slug }}">{{ event.title }}</a></li>
  {% endfor %}
  </ul>
© 2024 LocomotiveCMS Terms of use Privacy Policy
Back to top