Skip to main content

Templates

Fetch the display templates that control how pages, blocks and products render in your app.

More information about templates in the OneEntry admin panel: https://doc.oneentry.cloud/docs/category/templates (opens in a new tab)


Templates describe the structure and appearance of your entities outside the code. You create them in the OneEntry admin panel (Settings > Templates) and assign them to your components; the Templates module fetches them, so you can change how an entity renders by switching its template, without a redeploy.

Quickstart​

Initialize the module from defineOneEntry:


const { } = defineOneEntry(
, {
}
);

Fetch templates and read their configuration:

// Fetch all templates of a given BlockType.
const productTemplates = await Templates.getTemplateByType("product", "en_US");
if ('statusCode' in productTemplates) throw new Error(productTemplates.message);

productTemplates.forEach((tpl) => {
console.log(tpl.identifier, tpl.title, tpl.generalTypeName);
});

// Or fetch a single template by its marker.
const card = await Templates.getTemplateByMarker("product_thumbnail", "en_US");
if ('statusCode' in card) throw new Error(card.message);
console.log(card.attributeValues);

Template object​

A template (ITemplateEntity) is a display configuration that defines how content appears:

  • generalTypeName: the entity category the template applies to, a BlockType such as product, common_page, common_block, form, or order.
  • title: the template name shown in the admin panel. Not unique.
  • identifier: the unique marker you reference in code.
  • attributeValues: a map of attribute values keyed by marker.
  • attributeSetIdentifier: the associated attribute set, if any.
interface ITemplateEntity {
id: number; // Unique identifier
title: string; // Template name (non-unique)
identifier: string; // Template marker (unique)
generalTypeId: number; // Type ID reference
version: number; // Version number
generalTypeName: BlockType; // General type name
attributeSetIdentifier: string | null; // Associated attribute set
attributeValues: IAttributeValues; // Map of attribute values keyed by marker
position: number; // Sort position
}

Template types​

A template's type is its generalTypeName, a BlockType value. getTemplateByType(type) accepts the same BlockType. Common values:

TypeEntity CategoryExample Use Cases
productProductsProduct detail, product display
catalog_pageCatalog pagesCategory listing, search results
common_pageRegular pagesBlog post, landing page, about page
error_pageError pages404 page, 500 page, custom error layouts
common_blockContent blocksContent cards, banners, sections, widgets
formFormsContact form, registration form, survey
orderOrdersOrder confirmation, order history
serviceService entitiesService-related content. Not accepted by getTemplateByType() yet

getTemplateByType('service') currently fails with a 400 error (type must be a valid enum value). This is a known platform issue.

Creating templates​

The SDK is read-only: you can't create templates through it. Create them in the OneEntry admin panel (Settings > Templates). Each template needs a Name (not unique), a unique Marker, and a Type (the entity category it applies to).

Fetching templates​

Reference templates in code by marker (identifier), not by ID: markers are stable across environments, IDs aren't. Use getTemplateByMarker(marker) for a single template, getTemplateByType(type) for all templates of one BlockType, and getAllTemplates() for every template grouped by type.

getTemplateByMarker() resolves to an IError (statusCode: 404) when no template has that marker. getTemplateByType() resolves to an empty array when a type has no templates, and to an IError (statusCode: 400) when the type isn't a valid BlockType. getAllTemplates() currently resolves to an empty object ({}) instead of an IError when the request fails (for example, with an invalid app token), so check for an empty result.

Templates rarely change, so they're good candidates for caching.

Templates vs. TemplatePreviews​

Templates configure general content display (pages, blocks, products). TemplatePreviews set how OneEntry crops images into previews for image attributes and file uploads.


Methods​

MethodDescriptionUse Case
getAllTemplates()Fetch all templates grouped by typeList all available templates
getTemplateByType()Fetch templates by entity typeFetch templates for a specific entity type
getTemplateByMarker()Fetch a template by markerFetch a specific template configuration