Skip to main content

Build your first banner

In about 15 minutes you'll create a banner content type, publish a banner, and render it on a page with JavaScript or React. This is what you'll end up with, using your own image and text:

A banner with the headline "Summer, sorted", a strapline and a Shop the edit button over a sunset image

You'll need an Amplience account. If you don't have one yet, start a free Developer Trial, or skip to step 3 and try the code with our sample banner.

Step 1: Create the banner content type
Link copied!

A content type decides which fields authors fill in when they create content. The banner has six properties:

PropertyTypeUsed for
headlineText, requiredThe main heading
straplineTextThe line under the heading
imageImage from Amplience DAM, requiredThe background image
imageAltTextDescribes the image for screen readers
ctaLabelTextThe button label
ctaUrlText (URL)Where the button goes

The code in step 3 expects these names. If your content type ends up with different ones, that's fine. You can generate code for it or change one function yourself.

Choose how you want to create it:

  1. In Amplience CMS, open Development > Content type schemas and choose Create with assistant.

  2. Paste this prompt:

    Create a content type called "Get started banner" for a full-width hero banner.
    Use exactly these property names, all at the top level with no nested objects:

    - headline: text, required, up to 60 characters
    - strapline: text, optional, up to 120 characters
    - image: an image from Amplience DAM, required
    - imageAlt: text, optional, alt text that describes the image
    - ctaLabel: text, optional, the button label, up to 30 characters
    - ctaUrl: text, optional, a URL the button links to
  3. Check the suggested properties against the table above, then save and register the content type.

Step 2: Create and publish a banner
Link copied!

  1. Open Content, choose Create content and select Get started banner.

  2. Fill in the fields. Use your own text or these values:

    FieldValue
    headlineSummer, sorted
    straplineLight layers for long days outside.
    imageAltDescribe the image you choose
    ctaLabelShop the edit
    ctaUrlhttps://example.com/summer
  3. Choose any image from Amplience DAM. The text is white over a dark fade, so most photos work.

  4. Save the banner. From the Save menu, choose Delivery key, enter get-started-banner and save.

  5. Choose Publish.

You'll also need your hub name. You'll find it in the content item's properties, as part of the delivery URL, or under Settings > Properties.

Step 3: Render the banner
Link copied!

Install the Content Delivery SDK:

npm install dc-delivery-sdk-js

The window below runs the code against our sample banner. Change HUB_NAME to your hub name to see your own banner. If you used a different delivery key, change DELIVERY_KEY too. The banner updates as you type.

Edit the code, or paste in your own. Reset code puts the original back.

When it works, copy the code for your framework, along with the CSS, into your project.

Add an element for the banner and load the CSS and script on your page:

<link rel="stylesheet" href="banner.css" />
<div id="banner"></div>
<script type="module" src="banner.js"></script>

The import from dc-delivery-sdk-js needs a bundler such as Vite. Without one, use the pre-bundled SDK.

The numbered comments in the code match these steps:

  1. Connect. ContentClient connects to your hub.
  2. Map. toBanner picks the fields the banner needs from the content item. The SDK turns the image property into an object with a URL builder, so image.url().width(1600).build() returns an image resized for the banner.
  3. Render. The markup uses each field. Optional fields are left out when they're empty.
  4. Fetch. getContentItemByKey retrieves the published banner by its delivery key.

Make it yours
Link copied!

Generate code for your content type
Link copied!

If your content type has different properties from the table in step 1, or you've added your own, paste its schema below and Ask AI will write the rendering code for it. You'll find the schema in the schema editor's code view. You can also paste a content item, logged with console.log(JSON.stringify(item.body)).

Write the code in

Leave these blank to fill them in later, in the code.

Add your hub name and delivery key too if you have them. You can leave them blank and fill them in later. When Ask AI replies, its code is loaded into the window in step 3, replacing the component and the CSS. If you gave your hub name and delivery key, the window renders your content straight away. Otherwise, change HUB_NAME and DELIVERY_KEY in the code. If you'd rather keep what was in the window, choose Undo. Follow-up requests in the same Ask AI conversation, such as changing a color, are loaded the same way. It follows the same structure as the tutorial code, so the rest of this section still applies to it.

If your property names are different
Link copied!

The assistant may name or group properties differently from the table in step 1. Only toBanner needs to change, and the markup stays the same. Find the line for the field and point it at your property:

Your content type hasChange the line in toBanner to
title instead of headlineheadline: content.title,
A cta object with label and urlctaLabel: content.cta?.label, and ctaUrl: content.cta?.url,
A background object with image and altimageUrl: content.background?.image?.url().width(1600).build(), and imageAlt: content.background?.alt ?? '',

To see exactly what your content item returns, log it before mapping: console.log(item.body).

Add a field
Link copied!

Say you want a short label above the headline, such as "New season". Adding a field takes three changes.

  1. Add it to the content type. Ask the assistant: Add an optional text property called eyebrow, up to 30 characters. Or add it to the schema:

    "eyebrow": { "title": "Eyebrow", "type": "string", "maxLength": 30 }
  2. Map it in toBanner:

    eyebrow: content.eyebrow,
  3. Render it above the headline:

    ${banner.eyebrow ? `<p class="banner__eyebrow">${safe(banner.eyebrow)}</p>` : ''}

Then add a style for it to banner.css:

.banner__eyebrow {
margin: 0 0 0.5rem;
font-size: 0.875rem;
font-weight: 600;
}

To remove a field, delete its line from toBanner and from the markup.

If the banner doesn't appear
Link copied!

The item isn't found. Check that the banner is published, that the delivery key matches DELIVERY_KEY exactly, and that HUB_NAME is your hub name. A new publish can take a few seconds to reach the Content Delivery API.

The text appears but the image doesn't. Check that the banner has an image selected. If your image property has a different name, update the imageUrl line in toBanner.

A field is missing. Log item.body and compare the property names with toBanner.

Next steps
Link copied!