Define a content type

LocomotiveCMS allows you to create content types without imposing a specific pre-built structure.

Your content types can then be used directly in your Liquid templates.

Generate it with Wagon

Wagon provides you the generate command to help you to create the YAML file describing your content type.

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

It will generate 2 files:

  app/content_types/events.yml
  data/events.yml

The first one contains the content_type declaration, the second contains sample data so that you can test your site with fake data. Some random data have been added.

Important: Use plural for your content type names.

Note: The available field types are : string, text, file, select, boolean, date, datetime, tags, integer, float, belongs\to, has_many, many_to_many.

Description of the YAML file

Here are the properties you can use on app/content_types/*.yml files. All the content types should be plural filenames.

Example of an Article content type:

  name: Articles
  description: Just a simple blog module
  order_by: posted_at
  order_direction: desc
  slug: articles
  label_field_name: title
  fields:
  - title:
      label: Title
      type: string
      hint: The title of the article
  - posted_at:
      label: Posted at
      type: date
      hint: The date when the article has been or will be posted.
  - body:
      label: Body
      type: text
      text_formatting: html
      hint: The body of the article
Property Description
name String value. Public name of the content type. Use plural.
slug String value. Internal name of the content type. Use plural and lower case, no space.
description String value. Explanation for the backoffice
label_field_name Field slug. Default field slug (generally title), it must be a string field
order_by Any field slug or 'manually' or 'created_at'. Changes the order of content entries in backoffice and content entries.
order direction 'asc' or 'desc'
group_by Field slug. Display entries grouped by the given field in the backoffice
fields List of fields whose structure is described below

Fields types

The minimal structure:

field_slug:
  type: <type>
  option: value

Common properties of fields

Property Description
label Required. Label for the backoffice
required default false
hint Displays a hint in the backoffice.
localized Default false. Makes this field localized: each language will have a different value.

Depending on the field type, you can provide one or more options.

Property Description
string Standard string field
text Longer field. Use with text_formatting: html option to display a wysiwyg editor in the backoffice.
select Add select_options: ["value 1", "value 2"] or for multilingual:
select_options:
  en: ["Value 1", "Value 2"]
  fr: ["Valeur 1", "Valeur 2"]
file No option, get the url with product.the_photo.url in your code.
integer Integer field. No option.
float Float field. No option.
date Date field. No option.
date_time DateTime field. No option.
boolean true or false field. No option.
tags Tags field. No option.
belongs_to Example with product belongs to a category, field 'category' in products.yml: class_name: categories
has_many Declare a relationship between 2 content types. Example with category has many products (field 'products' in categories.yml):
class_name: products
inverse_of: category
ui_enabled: true
Notice the plurals and singulars.
many_to_many Example with products has and belongs to many categories. In products.yml:
class_name: products
inverse_of: category
ui_enabled: true
© 2024 LocomotiveCMS Terms of use Privacy Policy
Back to top