Modify existing templates and manage template lifecycle in headless Node.js environments using CE.SDK Engine.
Templates evolve as designs change. You might need to update branding, fix content errors, or remove outdated templates from your library. CE.SDK Engine provides APIs for adding, editing, and removing templates from asset sources in server-side automation workflows.
This guide covers how to add templates to asset sources, edit template content, remove templates, and save updated versions.
Adding Templates#
First, create a local asset source to store your templates:
// Create a local asset source for managing templatesengine.asset.addLocalSource('my-templates');Next, create your template content using block APIs:
// Create the template with text blocksconst titleBlock = engine.block.create('text');engine.block.replaceText(titleBlock, 'Original Template');engine.block.setFloat(titleBlock, 'text/fontSize', 14);engine.block.setWidthMode(titleBlock, 'Auto');engine.block.setHeightMode(titleBlock, 'Auto');engine.block.appendChild(page, titleBlock);
const subtitleBlock = engine.block.create('text');engine.block.replaceText(subtitleBlock, 'Server-side template management');engine.block.setFloat(subtitleBlock, 'text/fontSize', 10);engine.block.setWidthMode(subtitleBlock, 'Auto');engine.block.setHeightMode(subtitleBlock, 'Auto');engine.block.appendChild(page, subtitleBlock);Then save the template and add it to the asset source using addAssetToSource(). Each template needs a unique ID, a label, and metadata containing the template URI and thumbnail:
// Save template content and add to asset sourceconst originalContent = await engine.scene.saveToString();engine.asset.addAssetToSource('my-templates', { id: 'template-original', label: { en: 'Original Template' }, meta: { uri: `data:application/octet-stream;base64,${originalContent}`, thumbUri: generateThumbnail('Original Template') }});The meta.uri field contains the template content as a data URI. The meta.thumbUri provides a thumbnail image for reference.
Editing Templates#
Modify template content using block APIs. You can update text, change images, adjust positions, and reconfigure any block properties.
// Edit the template content and save as a new versionengine.block.replaceText(titleBlock, 'Updated Template');engine.block.replaceText(subtitleBlock, 'This template was edited and saved');
const updatedContent = await engine.scene.saveToString();engine.asset.addAssetToSource('my-templates', { id: 'template-updated', label: { en: 'Updated Template' }, meta: { uri: `data:application/octet-stream;base64,${updatedContent}`, thumbUri: generateThumbnail('Updated Template') }});After editing, save the modified template as a new asset or update an existing one.
Removing Templates#
Remove templates from asset sources using removeAssetFromSource(). This permanently deletes the template entry from the source.
// Add a temporary template to demonstrate removalengine.asset.addAssetToSource('my-templates', { id: 'template-temporary', label: { en: 'Temporary Template' }, meta: { uri: `data:application/octet-stream;base64,${originalContent}`, thumbUri: generateThumbnail('Temporary Template') }});
// Remove the temporary template from the asset sourceengine.asset.removeAssetFromSource('my-templates', 'template-temporary');Saving Updated Templates#
To update an existing template, first remove it using removeAssetFromSource(), then add the updated version with addAssetToSource() using the same asset ID.
// Update an existing template by removing and re-adding with same IDengine.block.replaceText(subtitleBlock, 'Updated again with new content');const reUpdatedContent = await engine.scene.saveToString();
engine.asset.removeAssetFromSource('my-templates', 'template-updated');engine.asset.addAssetToSource('my-templates', { id: 'template-updated', label: { en: 'Updated Template' }, meta: { uri: `data:application/octet-stream;base64,${reUpdatedContent}`, thumbUri: generateThumbnail('Updated Template') }});
// Notify that the asset source contents have changedengine.asset.assetSourceContentsChanged('my-templates');After updating templates, call assetSourceContentsChanged() to notify that the asset source contents have changed.
Best Practices#
Versioning Strategies#
When managing template updates in server-side automation, consider these approaches:
- Replace in place: Use the same asset ID to update templates without changing references. Existing designs using the template won’t break.
- Version suffixes: Create new entries with version identifiers (e.g.,
template-v2). This preserves old versions while introducing new ones. - Archive old versions: Move deprecated templates to a separate source before removal. This maintains a history without cluttering the main library.
Batch Operations#
When adding, updating, or removing multiple templates, call assetSourceContentsChanged() once after all operations complete rather than after each individual change. This minimizes notification overhead in automated workflows.
Template IDs#
Use descriptive, unique IDs that reflect the template’s purpose (e.g., marketing-banner-2024, social-post-square). Consistent naming conventions make templates easier to find and manage programmatically.
Thumbnails#
Generate meaningful thumbnails that accurately represent template content. Even in server-side workflows, thumbnails are useful when templates are later displayed in browser-based asset libraries.
Memory Considerations#
Templates stored as base64 data URIs remain in memory. For server-side batch processing with many templates, consider storing template content externally and using URLs in the meta.uri field instead of inline data URIs.
Cleanup#
Always dispose the engine when done to free resources.
// Always dispose the engine to free resourcesengine.dispose();API Reference#
| Method | Description |
|---|---|
engine.asset.addLocalSource() | Create a local asset source |
engine.asset.addAssetToSource() | Add template to asset source |
engine.asset.removeAssetFromSource() | Remove template from asset source |
engine.asset.assetSourceContentsChanged() | Notify of asset source changes |
engine.scene.saveToString() | Save scene as base64 string |
engine.scene.loadFromString() | Load scene from base64 string |