Lab - Add a Subcategory Listing in Stencil
Lab - Add a Subcategory Listing in Stencil
Lab - Add a Subcategory Listing in Stencil
This lab provides a comprehensive walkthrough of customizing your storefront’s category pages. You will learn how to add dynamic subcategory listings, giving you practical experience with Stencil templating. You’ll also learn how to create and implement custom Sass for styling and how to extend your theme’s settings to control the presentation of your new features.
In this lab, you will:
Prerequisites
Catalog Data Prerequisites
In order to test the subcategory listing feature, you’ll need to ensure your store’s catalog data matches the following criteria:
You can edit your catalog to meet these criteria in the store control panel, under Products. In the Product Categories tab, create categories as necessary, making sure to specify a parent category to nest one category within another.

Use the product grid interface in _Products > View to assign products to the various categories.

In this simple Stencil customization, you’ll modify the category page template to display a listing of subcategories, with each displaying a product count and linking to the appropriate category URL.

Any category with subcategories will display this presentation instead of the typical product list, while a category without subcategories will display a product listing as usual. This means the shopper navigation experience will be to “drill down” into a category hierarchy until reaching a specific list or products.
In the local project where you initially cloned Cornerstone, make sure to start the Stencil CLI server process if it’s not running already.
?debug=context to the end of the URL. (Example: http://localhost:3000/shop-all?debug=context)This represents the schema of the data available to your Stencil category template. You will be able to reference category.subcategories within Handlebars expressions to access an array of subcategory objects.
Rather than build your subcategory presentation directly in the category page template, you will create a dedicated component for it.
components/category/subcategory-listing.html.Note that the initial Stencil object referenced for the loop in a Handlebars expression is subcategories, rather than category.subcategories as might be expected based on the schema you observed in the debugging output. This should be cleared up once you add the import for this component file.
This code uses a Handlebars “each” expression to iterate over the array of subcategories. Each subcategory object has properties including url, name, and product count, which you can see are referenced directly within the “each” expression. Within this loop, the Handlebars context is that of each object in the loop.
Finally, note that some values are referencing Stencil object data with a syntax like ../theme_settings.default_image_product. The ”../” in this reference operates similarly to typical file path syntax, accessing an object called theme settings_in the context “above” the current context (in this case, outside the loop). In this case, we are making use of some existing config settings related to lazy load behavior and defaults for the built-in responsive image template.
This new addition demonstrates how Handlebars context can be changed between templates. While category.subcategories is available from the top level context, this is being “passed” into the component template with the simple alias subcategories. Within the template being imported here, this makes the object data available using that variable name.
This SCSS snippet is using a few built-in mixins, functions, and variables provided by Cornerstone, the Citadel framework, and the underlying Foundation framework in order to implement a standard grid layout and reference common style values. Examples include the u-listBullets mixin that eliminates default bullet points for unordered lists, the breakpoint and spacing functions (supporting standardized values like “single” and “small”), and variables like $global-radius.
This styling will result in a single subcategory per row on the smallest viewports, two per row in viewports with a minimum width matching the “small” breakpoint, and three per row for those matching the “medium” breakpoint.
This components manifest file is, in turn, imported in the main theme SCSS file.

Currently, the background and border colors for the subcategory listing are hard-coded values. As a final enhancement, you’ll create theme settings for these in config.json instead. By relying on configurable settings, you’ll establish the foundation for easily creating theme variations with a different look and feel, as well as making these presentational elements editable in the Page Builder interface.
Any number of presentational aspects of your component might be made configurable by settings in the same way.
assets/scss/components/custom/subcategoryGrid/_subcategoryGrid.scssstencilColor function, referencing your new theme settings.To enhance the subcategory listing feature, consider the following additional exercises.
One of the primary uses of the configurable settings whose default values are found in config.json is to make these settings editable within Page Builder in the BigCommerce control panel. Exposing these settings here gives admin users the power to customize various aspects of the look and feel of the storefront without the need for code changes.
In order to make your new settings available in Page Builder, two more files must be edited: schema.json, which defines the available settings and the form controls that will be presented for them, and schemaTranslations.json, which provides localized labels and strings consumed by the schema definitions.
Examine the existing settings in these files, and add the appropriate customizations for your two color fields. Consider making other presentational aspects of the subcategory listing component editable with other settings as well. Keep the following in mind:
If our categories are in a many-leveled hierarchy, we might want our subcategory listing to give shoppers a way to drill down multiple levels in one shot. We could do this by listing each of the subcategories’ own child categories within its card. However, this is not part of the data available in the category.subcategories Stencil objects.
In this case, a query to the GraphQL Storefront API can be used as an alternative method for obtaining the data to work with in your templates, and the query needed here can be embedded directly in the YAML front matter of the category template.
In preparation for pivoting to GraphQL-provided data within your component, study the site.categoryTree query in the GraphQL Storefront API reference and read about the use of GraphQL within front matter in the BigCommerce docs.