BoltCM

Sophisticated, Lightweight and Simple

Jump to docs navigation
Edit on GitHub

Templating » Using the Fields-tag

When developing a theme, you often know which fields there are, and you carefully place them in the templates where you want them to appear, and what the markup looks like. On the other hand, in some cases you simply can't do this for each separate field, for a number of reasons:

In these cases, you cannot specify all of the fields like {{ record.title }} separately, because you simply don't know the exact structure of the ContentType. For this very usecase, Bolt provides a tag to output these fields when you're working on a template without having to know exactly what's in the ContentType. This is the {{ fields() }}-tag. Basic usage is straightforward:

{% extends 'partials/_master.twig' %}

{% block main %}

    <h1>{{ record.title }}</h1>

    {{ fields() }}

{% endblock main %}

This will output a basic page, with the 'title' of the record in an <h1>-tag, followed by the rest of the fields, in a generic layout.

The {{ fields() }}-tag comes with a number of options, to determine which fields are being output and in what format.

You can use all of these options in Twig, but in practice, it'll be much more legible to use 'named parameters' for this.

{{ fields(exclude = ['teaser', 'body']) }}

Setting the template

All fields are rendered by twig itself. In your theme, you can override the way these fields look, by setting the template to use for the field definitions.

By default, fields() uses the file app/theme_defaults/_sub_fields.twig. To override this, either copy the file to the root of your theme folder, or place and rename as you like, and specify it explicitly:

{{ fields(template = '_partials/_sub_fields.twig') }}

Using skip_uses

By default, fields() skips the fields that are used by the slug. This is why the first example on this page works as-is. We manually output the title in an <h1>-tag, and let fields() handle the rest. Doing this assumes that the ContentType looks something like this:

pages:
    name: Pages
    singular_name: Page
    fields:
        title:
            type: text
            class: large
            group: content
        slug:
            type: slug
            uses: title

The fields()-tag looks at the uses in the ContentType, and skips the fields that are mentioned there.

In case this is not desired behaviour, just use this:

{{ fields(skip_uses = false) }}

Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on IRC.
Spotted a typo, or have something to add? Edit this page on GitHub.