TTWeb Basics 3. Content Model Instances

Model instances (unlike definitions) are designed to be easily modifiable by non-technical users. Once a definition has been created, it’s instantly available for management under the Content section of a draft.

Dynamic Page Generation

With TravelTripper.io plugins, Jekyll can auto-generate a page for each instance of a model. There are two steps involved in setting up auto generation:

Create a page template in _layouts used for page generation. The values of the model attributes are accessible through tye liquid page variable: {{ page.attribute_name }} for example: {{ page.author.name }}.

Set up the page_gen section of _config.yml in your jekyll project to specify a model type, template and publish location, for example:

page_gen:
  - data: 'news'      # The name of the content model
    template: 'news'  # The name of the _layout file
    dir: 'news'       # The URL prefix
  - data: 'authors'
    template: 'author_bio'
    dir: 'about/our-writers'

This will take each instance of the news model and use the _layouts/news.html template to render a page into /news/ when the site is built. It will also take each instance of the authors model and use the _layouts/author_bio.html template to render a page into /about/our-writers/.

For content that may expire, it is strongly recommended that you create logic in your tepmlate that

  1. Uses a model attribute such as “published” or “expires_at” date-time to determine the page content.
  2. If time sensitive, output the expires_at field as a javascript variable

This will allow you to have the page show the appropriate content to a user (e.g. “This event has passed, please see our /events/ page to find more”) but still keep the content indexed on the site.

Iteration (for loops)

You can iterate through the list of model instances, since they are stored as standard jekyll site data.

{% for news_item in site.data._models.news %}
  <h3>{{ news_item.title }}</h3>
  <p>{{ news_item.author.name }}</p>
{% endfor %}

Tip: If you’re iterating through a list of instances which are also being auto generated as pages, you can use the permalink liquid filter tag to return the URL for that page. For example: {{ news_item.author.id | permalink: model-dir: 'about/our-writers'}}