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.

1. Declare the "events" content type

cd ~/workspace/my_first_site
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
vi app/views/pages/events/content_type_template.liquid

๐Ÿ“˜

Use your own code Editor

You could replace vi by your preferred code editor (SublimeText, TextMate, ...etc).

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="{% path_to event %}">{{ event.title }}</a></li>
{% endfor %}
</ul>

If you want the editors to have a link to the list of events when they edit the index page, you need to wrap your code with the editable_model liquid tag like this way:

{% editable_model events, hint: 'some text' %}
  <ul>
  {% for event in contents.events %}
    <li><a href="{% path_to event %}">{{ event.title }}</a></li>
  {% endfor %}
  </ul>
{% endeditable_model %}

So, when your editors edit the index page, they will see a shortlink to the list of events.

1100