Section rendering API

Use the section rendering API to fetch HTML for theme sections through AJAX requests. This enables you to update page content dynamically by obtaining and replacing specific elements without reloading the entire page.

For example, you can implement pagination for research results using the section rendering API, allowing for page changes without a full page reload when switching between pages.


Request sections

You can use the sections query parameter to render specific page sections. The values of sections are unique IDs to identify sections. Refer to Obtain section IDs for how to get values of sections.

Separate the section ID values by commas, as shown in the following example:

?sections=sections--header-group__announcement-bar,sections--header-group__header

You can render sections on any page context by appending the sections parameter to any page URL. For example, if you make a request to the homepage using /?sections=sections--header-group__header, it will return the HTML fragment of the corresponding section to populate the page.

The response for the API is a JSON object, including the IDs and HTML files for each section. Refer to Example for a response sample.

Note: You cannot specify section settings through the section rendering API. If the section that you request exists in templates, the existing settings are applied. Otherwise, the default section settings are applied.

Example

The following code sample requests two sections using the JavaScript fetch method:

fetch('/?sections=sections--header-group__announcement-bar,sections--header-group__header').then((response) => {
const renderSections = response.json();
const announcementBarHtml = renderSections['sections--header-group__announcement-bar'];
const headerHtml = renderSections['sections--header-group__header'];
})

The response is as follows:

{
"sections--header-group__announcement-bar": "<div id=\"shopline-section-sections--header-group__announcement-bar\" class=\"section\">\n<!-- section content -->\n</div>",
"sections--header-group__header": "<div id=\"shopline-section-sections--header-group__header\" class=\"section\">\n<!-- section content -->\n</div>"
}

Tip: When rendering sections, existing query parameters governing the page display remain in effect. For example, suppose the initial product list on the homepage uses the parameter page_num=1. In that case, you can fetch the second page of products by requesting /collections/clothing/?page_num=2&section_id=template--collection__main-collection-products and append the products after the initial list on the page.

Error codes

Sections that fail to be rendered, such as due to non-existent themes, will return null in the JSON response. The response may have an HTTP 200 status code, but still contain one or more sections with null values. Be prepared to handle situations when null values are returned.


Request a single section

If you want to render a single section, you can use the section_id and sections query parameters to request the section. However, it is highly recommended that you use section_id to request a section.

?section_id=sections--header-group__announcement-bar

Similar to sections, you can use section_id to render a single section at any context on a page. If you use the section_id parameter to request a section, HTML for the section is returned.

<div id=\"shopline-section-sections--header-group__announcement-bar\" class=\"section\">\n<!-- section content -->\n</div>

Tip: You cannot specify the settings values for sections using the section rendering API. If the requested section exists in the theme templates, the theme's configuration is applied. Otherwise, the default configuration for the section is used.


Obtain section IDs

You can obtain section IDs in the following ways:

Extract section IDs from ID attributes

You can obtain a section ID from the ID attribute in an HTML element that represents a section. Take the following HTML snippet for example, you can extract the section ID from [section-id].

<div id="shopline-section-[section-id]" class="shopline-section">
<!-- section content -->
</div>

If you add a new section to the JSON template, a unique ID is generated for the section. For example, if two product list sections are added to the same page, each will have a unique ID. This allows you to identify and work with each section easily.

Was this article helpful to you?