Templates API
This is a technical summary of what is offered by our Templates API and how to programatically create and import .tco files.
The Templates API offers a way to create and import .tco files. It uses two main functions cs_export_documents and cs_import_tco. Let's go through them.
cs_export_documents
It contains 3 arguments
$idsthis can be an array of post ids or a singular post. These are the posts to export into a.tcofile. If a post contains components or media it will also export those as well with the export.$typeeitherdocumentortemplate. Iftemplateis used it will import to your templates tab. Usingdocumentwill create a new post or replace or ignore depending on the third argument$strategy. The default isdocument.$strategyeitheroriginalorreplace.originalwill not overwrite the post when imported twice.replacewill overwrite the data if the file has been imported twice.
// Using the defaults 'document' $type and 'original' $strategy
$fileLocation = cs_export_documents($YOUR_POST_ID);
// Using 'template' $type and 'replace' $strategy
$fileLocation = cs_export_documents([$YOUR_POST_ID, $YOUR_POST_ID2], 'template', 'replace');
// Function Signature
function cs_export_documents($ids = [], $type = 'document', $strategy = 'original');cs_export_documents_as_base64
This is a helper that uses cs_export_documents, but instead of returning a file location will return you a base64 string. This can be useful for using within JSON outputs. It uses the same arguments as cs_export_documents.
// Using the defaults 'document' $type and 'original' $strategy
$base64TCOFile = cs_export_documents_as_base64($YOUR_POST_ID);
// Function Signature
function cs_export_documents_as_base64($ids = [], $type = 'document', $strategy = '');cs_import_tco
Using the exports from above you can then import your file through cs_import_tco. This has a singular argument which is the file location. The return is the ID of the last document imported. Typically this is the main document you were exporting since exporting a document will also import media and components.
$ID = cs_import_tco($tcoFileLocation);See something inaccurate? Let us know