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, aBlockTypesuch asproduct,common_page,common_block,form, ororder.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:
| Type | Entity Category | Example Use Cases |
|---|---|---|
product | Products | Product detail, product display |
catalog_page | Catalog pages | Category listing, search results |
common_page | Regular pages | Blog post, landing page, about page |
error_page | Error pages | 404 page, 500 page, custom error layouts |
common_block | Content blocks | Content cards, banners, sections, widgets |
form | Forms | Contact form, registration form, survey |
order | Orders | Order confirmation, order history |
service | Service entities | Service-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
| Method | Description | Use Case |
|---|---|---|
| getAllTemplates() | Fetch all templates grouped by type | List all available templates |
| getTemplateByType() | Fetch templates by entity type | Fetch templates for a specific entity type |
| getTemplateByMarker() | Fetch a template by marker | Fetch a specific template configuration |
Related
- OneEntry Admin Panel - Content Templates: official admin panel documentation
- TemplatePreviews Module: preview templates for server-side image previews
- Products Module: products use product templates
- Pages Module: pages use page templates
- Blocks Module: blocks use block templates
- Forms Module: forms use form templates