Skip to content

Update basic "record and records" templating info. #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions docs/templating/record-and-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,62 @@ for that matter) are, is to use the `dump()` function:
{{ dump(record) }}
```

<a href="/files/content-example3.png" class="popup"><img src="/files/content-example3.png" width="500"></a>
<a href="/files/4.0.content-example3.png" class="popup"><img src="/files/4.0.content-example3.png" width="500"></a>

As you can see in the screenshot, a record of a ContentType is an `object`.
There are several benefits to this over a regular `array`. We can access the
fields like regular values, but we can also use specific functionality for
every object, without the need to define these separately.

In addition, Bolt ships with some Twig filters and functions to display the
content and all its related fields and values.

You can access regular fields in a record like these examples for either a
`page` or `entry` record:

```twig
{{ page.title }}
{{ page.text }}

Created on {{ entry.datecreated|date('Y-m-d')}}
Created on {{ entry.createdAt|date('Y-m-d')}}

The ContentType for this entry is {{ entry.contenttype.name }},
and it contains {{ entry.contenttype.fields|length }} fields.
The ContentType for this entry is {{ entry.contenttype }},
and it contains {{ entry.fields.count }} fields.
```

The real power comes from using the special functions that are defined for every
content record.

To get a link to the content:
To get a link to the content, use the `link` filter:

```twig
Link: <a href="{{ page.link }}">{{ page.title }}</a>
Link: <a href="{{ page|link }}">{{ page.title }}</a>
```

Get a short excerpt of the record:

```twig
<p>{{ page.excerpt(250) }}</p>
<p>{{ page|excerpt(250) }}</p>
```

Get the next and previous record:

```twig
{% set previous = page.previous() %}
{% set next = page.next() %}
{% set previous = page|previous() %}
{% set next = page|next() %}
```

The next and previous functions allow for additional parameters. For example,
you can base the next record on any field (this is `datepublish` by default),
filtered by a `where` clause, see [using where](content-fetching#using-where)
for more details.
you can base the next record on the ID (the default), but you could also get the
next page by publishing date.

```twig
{% set next = page.next('datepublish', {'status': page.taxonomy.status} ) %}
{% set next = page|next('publishedAt') %}
```

The complete list of available Twig filters for a record can be found
[here](../twig-components/filters) and [here](../twig-components/extras).

### Getting the type of a certain field

If you're iterating over an array of `record.fields`, it's sometimes useful to
Expand Down