Lab - Using an Array in a Schema
Lab - Using an Array in a Schema
Lab - Using an Array in a Schema
Summary
In the previous sections, you created widget template schemas with a single tab element. In this section, you will create a widget template that allows the content creator to select multiple products from the catalog, and display their product images in a grid. To allow the user to select multiple products, you will write a schema with an array containing multiple schemas, each with their own tab. In the end, you should be able to use Page Builder to create widgets that look like this:

In this lab you will
Prerequisites
In this lab, you will begin to build a product card grid. In more advanced applications, you could build on this template to create a functional “add all to cart” widget, but for now, the template is quite simple and only displays the product ID and a product image on each card. For an example of a more complex widget template utilizing arrays, look at the Carousel widget template available by default.
The widget template’s schema defines the inputs a content creator can use to configure a widget. Widget templates use Handlebars to access the configured values to determine the content, markup, and styles to display. In this section, you will write an array of schemas containing:

Throughout this section, you will write HTML and JSON to create the request body for a Create a Widget Template POST request. You may reference the following code sample during the following steps to check your work:

Example:

Example:
At this point, your schema should contain the same array, schema, and settings as this example.
Just as in the previous example, you will use Handlebars to access the widget’s data in the template markup. Unlike the previous example, the data is available as an array rather than a single object. To iterate through the array and access the data for each of the productId and image settings, the template will use the {{#each}} Handlebars block helper to render some content for each element in the array. For more information on the {{#each}} helper, see the official Handlebars documentation.
In this example, you will use a CSS flexbox layout to create a grid that displays three products per row. Feel free to spruce up the markup, but bear in mind that future examples may reference the template you create in this section. For a CSS flexbox quick reference, see a resource like css-tricks.com.
Example:
Example:
You should now have 8 lines of HTML:
At this point, you should have a line of code that matches the template in the example.
Having written the schema and markup, you are now ready to use the Widgets API to create a widget template. You will begin by writing the API request body, then you will use a REST client to create the template, then you will test your template in Page Builder.

Try dragging your custom widget onto the page. Assuming the schema is structured correctly, you should be able to modify the settings in the left sidebar. The end result should look something like this:


This warning indicates that there is an issue with the widget template schema. Examine your schema and review the BigCommerce Dev Docs. Specifically, ensure your schema has exactly one root element with type ‘array’, and that element has its own “schema” array with exactly one root tab element. All configurable settings should be contained in a section inside the tab.
For an extra challenge, try sprucing up the markup and CSS, or implement new settings to control the typography and styling of the widget.