Lab - Create a Widget Template to display Dynamic Data
Lab - Create a Widget Template to display Dynamic Data
Lab - Create a Widget Template to display Dynamic Data
Summary
In the previous section you used page builder widget UI inputs to configure static content including a product image and some text entered in Page Builder. In this section, you will combine Page Builder with your store’s GraphQL Storefront API to create a dynamic widget powered by GraphQL. The goal of this section is to create a widget template that dynamically displays a product’s default image and product description above a list of the product’s variants that displays the image, price, and SKU of each variant based on the Page Builder user’s product selection:

In this lab you will
Prerequisites
In this section you will focus on integrating data from GraphQL into your widget template such that when a shopper selects a product using a Product ID setting in the Page Builder UI, information about that product will be displayed in JSON on the page.

In the following steps, you will use the Storefront API Playground to test a GraphQL query that, given a product ID, requests the following information:
Until you integrate your query with your widget template you will need to manually supply the product ID to test it. Start by finding the product ID for your product.
Finding the right test product.
This example will work for any product, but you will have the best result if you use a product that has multiple variants, each with its own SKU and image.
KB Article - Variants
![]()
You should see a UI with some example queries where you can write a query in the left pane, configure query variables in the lower left, run the request, and see the results on the right. You may need to expand the query variables section in the lower left.

Be sure to replace the product’s ID (50918) in the example above with your test product ID.
You should see information about your product in the query results pane on the right:

Why does my result say “product”: null?
If you get a null result for your product query, that means that you, as an unauthenticated storefront API user, are not authorized to see that product. This could be because…
The resulting query should look like one of the following:
With newlines removed:
With newlines escaped:
Example: Here
At this point you should have the beginnings of a widget template that looks like this:
The widget template’s schema defines the inputs a content creator can use to configure a widget. When a widget template includes a GraphQL query, the schema is also used to populate the query variables with selections from other inputs (like a product ID). 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 a product ID input to select a product from the catalog.

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:
Use a text editor of your choice to write JSON for the following steps. These steps will walk you through creating the schema array in the Create a Widget Template request. To create the configurable fields in the Page Builder sidebar, the schema must include exactly one tab at the root level. To map the product ID from the user’s product selection to the $productId variable in the GraphQL query, the schema must also include exactly one hidden element on the root level. In this case, the tab contains one section which contains settings for heading text, image upload, image width, and text block content.

Example:
At this point, your schema should contain a single tab that looks like this.
At this point, your schema should contain both the tab and hidden elements like this example.
While the widget template schema is used to define the input and map the input’s value to the GraphQL query, the template’s markup will be used to define the structure and styling of the widget. Widgets access the results of a GraphQL query on the underscore (_) object. For example, to reference the GraphQL data, you can use the following Handlebars expression:
By default the data is an object rather than a string, so simply adding {{_.data}} to a template will result in output like “[Object object]”. In this example, we simply want to print the result in a widget so you will use the {{json}} Handlebars helper to convert the object to a string and triple curly brackets to prevent BigCommerce from converting special characters into HTML entities:
Example:
Having written the GraphQL query, 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.
Take note of the widget template’s id in the response body. You will use the id to update the widget in the next section.

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. When you first add the widget to the page, you should see a null result from your GraphQL query:

Begin typing a product name or SKU into the “Select a Product” field on the left sidebar. Once you select a product in the left sidebar the result should display the result of the GraphQL query like this:

Use the Save button in Page Builder to save your widget placement so that you can observe the effects of updating the widget template in the next section.

This warning indicates that there is an issue with the widget template schema. Examine your schema and review the Big Dev Docs. Specifically, ensure your schema has exactly two root elements: one tab element and one hidden element. All configurable settings should be contained by a section inside the tab.
Now that you have a widget template that displays dynamic information from GraphQL, it’s time to improve the template markup to display the product variant information in a human-readable list. When you update a widget template, you may choose to create a new template version which will allow you to push updates without changing existing widgets.
In this example, you will set create_new_version in the Update a Widget Template request so that the widget you created in the previous section is not changed but any new widgets created from the template will use the updated template. If you prefer to update all existing widgets that use the template, then you can omit create_new_version or set it to false.
See the Big Dev Docs for more information on versioning widget templates.
For brevity, this guide will not explain each line of code above but here are a few notable parts:
Using the updated markup, you will now update the existing widget template. In this section, you will create a new version of the widget template so that existing widgets are unchanged but any new widgets use the new template.

Try dragging your updated custom widget onto the page. Assuming the update was successful, you should be able to modify the settings in the left sidebar. When you first add the widget to the page, you should see the placeholder text in the template:
![]()
Begin typing a product name or SKU into the “Select a Product” field on the left sidebar. Once you select a product in the left sidebar the result should display a variants list:

For an extra challenge, try sprucing up the markup and CSS, adding “view product” and “add to cart” links, or implement new settings to control the typography and styling of the widget.