Template Content

On this page, you'll learn how to display content from the Prismic API in your Nuxt application. This page will also teach you how to use these methods:

  • $prismic.asHTML()
  • $prismic.asText()
  • $prismic.asDate()
  • $prismic.asLink()

And these components:

  • <prismic-rich-text />
  • <prismic-text />
  • <prismic-embed />
  • <prismic-image />
  • <prismic-link />
  • <slice-zone />

By the end of this page, you will have content from Prismic displayed in your app.


Intro to templating

Content from Prismic comes in more than a dozen types. Most of these are simple primitive values, like Numbers or Booleans. Others are more complex structured values, like Titles, Rich Texts, and Links.

The examples on this page

Most of the examples on this page assume that you have queried a single document from the Prismic API using the $prismic object and stored it in a variable called document, like so:

<script>
export default {
  async asyncData({ $prismic, params, error }) {
    const document = await $prismic.api.getByUID('page', params.uid)
    if (document) {
      return { document }
    } else {
      error({ statusCode: 404, message: 'Page not found' })
    }
  }
}
</script>

With simple content types, you can inject them directly into your application:

Copy
<span>{{ document.data.example_number }}</span>
<!-- <span>74.5</span> -->

For structured content types, we provide special utilities. To render Rich Text, for instance, we provide a Rich Text component:

Copy
<prismic-rich-text :field="document.data.example_rich_text" />

Further learning: content types

You can learn more about all of these content types in our Content Modeling documentation.

To get started, lets look at the structure of raw API response.

The structure of the API response

Multiple documents

When you make a standard query to the Prismic API, you will get a JSON object filled with results. Here is a truncated example of an API response containing some simple content types:

Copy
{
  page: 1,
  // ...
  results: [
    {
      uid: 'example_document',
      // ...
      data: {
        example_date: '2020-12-10',
        example_timestamp: '2020-12-10T04:05:09+0000',
        example_color: '#c7ab5d',
        example_number: 74.5,
        example_key_text: 'Example Key Text Value',
        example_select: 'North',
        example_boolean: true,
      }
    },
    // ...
  ]
}

So, if you store the response in a variable called "response," you might access a single data point like this:

Copy
response.results[0].data.example_key_text // 'Example Key Text Value'

In Nuxt, you might use that like this:

Copy
<div>
  {{ response.results[0].data.example_key_text }}
</div>

Or, more likely, you would loop over the results array and template each item, like this:

Copy
<ul>
  <li v-for="document in response.results" :key="document.id">
    {{ document.data.example_key_text }}
  </li>
</ul>

A single document

Unlike most queries, the helper functions $prismic.api.getSingle(), $prismic.api.getByUID(), and $prismic.api.getByID() will return the data for an individual document directly.

Here's a truncated example of the response for one of those queries:

Copy
{
  uid: 'about',
  // Metadata for this result
  // ...
  data: {
    example_date: '2020-12-10',
    example_color: '#c7ab5d',
    example_key_text: 'Example Key Text Value',
    // More content
    // ...
  }
}

Notice that the document is not nested in an array of results within a query response object, like in a query for multiple documents.

With a single document query, you might access your Color field like this:

Copy
document.data.example_color
// '#c7ab5d'

Slices

Slices are repeatable, rearrangeable content sections. Slices are found in the body section of your document:

Copy
// document.data.body
[
  {
    slice_type: 'title_slice',
    slice_label: null,
    items: [{}],
    primary: {
      title: 'Some colorful numbers and birthdays'
    }
  },
  {
    slice_type: 'birthday_slice',
    slice_label: null,
    items: [{}],
    primary: {
      person: 'Alan Turing',
      birthday: '1912-06-23'
    }
  },
  {
    slice_type: 'birthday_slice',
    slice_label: null,
    items: [{}],
    primary: {
      person: 'Grace Hopper',
      birthday: '1906-12-09'
    }
  }
]

Coding Slices

Slice components receive four props, which you can use:

  • slice: The content of the Slice object being rendered.
  • index: The index of the Slice within the Slice Zone.
  • slices: The list of all Slices objects in the Slice Zone.
  • context: Arbitrary data passed to the <SliceZone>'s context prop.

Because defining props in Vue.js is verbose, we provide a helper function to do so: getSliceComponentProps(). With it, a simple Slice component could look like this:

slices/ExampleSlice/index.vue
Copy
<template>
  <PrismicRichText :field="slice.primary.text" />
</template>

<!-- Options API -->
<script>
import { getSliceComponentProps } from '@prismicio/vue/components'

export default {
  // The array passed to `getSliceComponentProps` is purely optional and acts as a visual hint for you
  props: getSliceComponentProps(['slice', 'index', 'slices', 'context']),
}
</script>

Every Slice may have a "non-repeatable" and a "repeatable" section. The repeatable section functions just like a Group field.

A screenshot from Slice Machine showing code snippets.
You can use the code snippets provided by Slice Machine to template your Slice.

When modeling your Slices, Slice Machine provides code snippets that you can copy-paste into your Vue component to template your Slice.

Rendering the Slice Zone

The @nuxtjs/prismic module provides a <SliceZone> component to render Slices. Import the Slices from your slices directory and pass them to the SliceZone component along with a slices object from a document from the Prismic API.

pages/index.vue
Copy
<template>
  <slice-zone :components="components" :slices="document.data.slices" />
</template>

<script>
import { components } from '~/slices'

export default {
  data: function () {
    return { components }
  },
  async asyncData({ $prismic, params, error }) {
    const document = await $prismic.api.getByUID(
      'example-custom-type',
      'example-uid'
    )

    if (document) {
      return { document }
    } else {
      error({ statusCode: 404, message: 'Page not found' })
    }
  },
}
</script>

Simple content types

The simple content types are:

  • Boolean
  • Color
  • Date
  • Timestamp
  • Number
  • Key Text
  • Select

These are represented as either a string, a number, or a boolean, and you can access them directly, like so:

Copy
document.data.example_number
// 74.5

Note: Working with dates

Dates come from the API as a string, in the formay YYYY-MM-DD. To convert a date string to a JavaScript Date object, you can use the built-in $prismic.asDate() method:

Copy
document.data.example_date
// '2020-12-10'

$prismic.asDate(document.data.example_date)
// Thu Dec 10 2020 01:00:00 GMT+0100 (Central European Standard Time)

Geopoints

The Geopoint content type is served as an object with two properties: latitude and longitude. This is the structure of a geopoint:

Copy
// document.data.example_geopoint
{
  latitude: 48.85392410000001,
  longitude: 2.2913515000000073
},

You can access these properties directly:

Copy
document.data.example_geopoint.latitude
// 48.85392410000001

document.data.example_geopoint.longitude
// 2.2913515000000073

Embeds

This is the structure of an Embed field:

Copy
// document.data.example_embed
{
  version: "1.0",
  url: "https://prismic.io",
  type: "link",
  title: "Make your website editable for the whole team - Prismic",
  provider_name: null,
  thumbnail_url: "https://images.prismic.io/prismic-website/6e49007fec52f99861047cdd55b3ab190ea04295_dev-landing-page-image.png?auto=compress,format",
  html: "<div data-type=\"unknown\"><a href=\"https://prismic.io\"><h1>Make your website editable for the whole team - Prismic</h1><img src=\"https://images.prismic.io/prismic-website/6e49007fec52f99861047cdd55b3ab190ea04295_dev-landing-page-image.png?auto=compress,format\"><p>Choose your technology. Use the API to fetch content. Empower your content team.</p></a></div>",
  embed_url: "https://prismic.io/"
},

You can template an Embed field with the Embed component:

Copy
<prismic-embed :field="document.data.example_embed" />

Images

In Prismic, you can define an image's alt text, copyright, and alternate crops — all of which can also have their own alt text. All of this information is served in the API response. A simple Image field might look like this:

Copy
// document.data.example_image
{
  dimensions: {
    width: 1920,
    height: 1302
  },
  alt: null,
  copyright: null,
  url: "https://images.prismic.io/sm-20201204-2/7d1fba99-5bec-4d59-b8eb-402706e2d36c_a-pril-62u95KgB49w-unsplash.jpg?auto=compress,format"
},

You can template an image with the image component:

Copy
<prismic-image :field="document.data.example_image" />

Rich Text and Titles

Rich Text and Titles are delivered in an array that contains information about the text structure. Here's an example of a Rich Text field (Title fields follow the same format):

Copy
// document.data.example_rich_text
[
  {
    type: "paragraph",
    text: "Example Rich Text Value",
    spans: [
      {
        start: 8,
        end: 17,
        type: "strong"
      }
    ]
  }
],

@prismicio/vue provides three ways to work with text.

Rich Text component

Here's how to render Rich Text or a Title with the Rich Text component (HTML Serializer and wrapper are optional):

Copy
<prismic-rich-text 
  :field="document.data.example_rich_text" 
  :htmlSerializer="optionalHtmlSerializer" 
  wrapper="article" 
/>

What is an HTML Serializer?

The HTML Serializer defines the markup of your Rich Text. (For example: italic text should have <em> tags.) @prismicio/vue contains a default HTML Serializer for standard markup, but you can create your own HTML Serializer if you want to customize your markup.

Text component

Here's how to render Rich Text or a Title with the text component (wrapper is optional):

Copy
<prismic-text 
  :field="document.data.example_rich_text" 
  wrapper="article" 
/>

Rich Text helper function

Here's how to render Rich Text or a Title with the Rich Text helper function:

Copy
<div v-html="$prismic.asHTML(document.data.example_rich_text, optionalHtmlSerializer)" />

Text helper function

Here is how to get plain text from a Rich Text or title field with the plaintext helper function:

Copy
<div>{{ $prismic.asText(document.data.example_rich_text) }}</div>

Links in Rich Text

Links in Rich Text are resolved with the Route Resolver and/or Link Resolver function defined in your Nuxt app setup. See more info on Link Resolving below.

Links and Content Relationships

The Link field allows you to link to an external webpage, an internal Prismic document, or an item in your Media Library (like a PDF). The Content Relationship field allows you to link specifically to an internal Prismic document.

Here's an example Content Relationship (a Link takes a similar format):

Copy
// document.data.example_content_relationship
{
  id: "X9C65hEAAEFIAuLo",
  type: "page",
  tags: [],
  slug: "another-document",
  lang: "en-us",
  uid: "another-document",
  link_type: "Document",
  isBroken: false
},

There are two things that you might want to do with a link:

  • Link to another page or media item, internally or externally
  • Pull in content from another document

Here's how to do those two things:

Link to another page

Internal linking always requires a Route Resolver or a Link Resolver, which you should have created in the Define Routes step. Here is how to create a link with the link component:

Copy
<prismic-link :field="document.data.example_link">
  Example Link
</prismic-link>

Here is how to create a link with the link helper function:

Copy
<router-link :to="$prismic.asLink(document.data.example_link)" />
  Example Link
</router-link>

Pull in content from another document

To pull in content from another content, you must fetch that content in your API Query, using the graphQuery or fetchLinks option. To do so, add an options object to your query, with fetchLinks as a key, and the field that you want to fetch as the value. The field is formatted as [custom-type].[field]:

Copy
$prismic.client.getByUID('post', 'hello-world', { fetchLinks: 'page.example_key_text' })

You can learn more about how to use these options in the @prismicio/client version 5 Technical Reference.

Once you have adjusted your API query, the linked content will appear in a "data" object nested in the Link or Content Relationship field:

Copy
// document.data.example_content_relationship
{
  id: "X9C65hEAAEFIAuLo",
  type: "page",
  tags: [],
  slug: "another-page-title",
  lang: "en-us",
  uid: "page-2",
  data: {
    example_key_text: "Another Page's Key Text"
  },
  link_type: "Document",
  isBroken: false
},

You can then template that content as usual:

Copy
<span>{{ document.data.example_content_relationship.data.example_key_text }}</span>
<!-- <span>Another Page Key Text</span> -->

Groups

The Group field is a repeatable collection of fields. This is useful for a data point that might have multiple instances, such as document categories or authors.

A Group field renders an array of content groups:

Copy
// document.data.example_group
[
  {
    example_boolean_in_group: true,
    example_number_in_group: 3,
    example_key_text_in_group: "dog"
  },
  {
    example_boolean_in_group: false,
    example_number_in_group: 7,
    example_key_text_in_group: "cat"
  },
],

To template a Group, you can use a v-for:

Copy
<ul>
  <li v-for="item in document.data.example_group" key="item.example_key_text_in_group">
    {{ example_key_text_in_group }} - {{ example_number_in_group }}
  </li>
</ul>

Metadata

An API response also contains metadata for the API response and for the individual result. Here's a truncated example for a standard query of multiple documents, as discussed above:

Copy
{
  // Response metadata
  page: 1,
  results_per_page: 20,
  results_size: 3,
  total_results_size: 3,
  total_pages: 1,
  next_page: null,
  prev_page: null,
  results: [
    {
      // Document metadata
      id: 'YBQvbxUAACUAa5jh',
      uid: 'example-uid',
      type: 'page',
      href: 'https://your-repo-name.cdn.prismic.io/api/v2/documents/sear...',
      tags: [],
      first_publication_date: '2021-01-29T15:53:23+0000',
      last_publication_date: '2021-01-29T15:53:23+0000',
      slugs: ['hello-world'],
      linked_documents: [],
      lang: 'en-us',
      alternate_languages: [],
      data: {
        // Document data
        example_date: '2020-12-10',
        example_timestamp: '2020-12-10T04:05:09+0000',
        // ... More document data
      }
    },
    // ... More documents
  ]
}

The response metadata is often used for more advanced query functions, such as pagination.

The metadata for a single document includes some useful information, such as the document's UID and published date.



Was this article helpful?
Not really
Yes, Thanks

Can't find what you're looking for? Spot an error in the documentation? Get in touch with us on our Community Forum or using the feedback form above.