Template & Document Lifecycle¶
The OdtTemplate facade owns one working ODT document created from an original template source.
For classic template processing, the normal lifecycle is:
save() does not call render(). Structured and Writer-native mutations may use their own lifecycle and still require an explicit save().
__construct()¶
Recommended · Template & Document
Creates an OdtTemplate instance, loads the supplied ODT package immediately, prepares the working document, and registers shutdown cleanup.
Parameters¶
| Parameter | Type | Description |
|---|---|---|
$templatePath |
string |
Path to the source .odt template. |
Lifecycle and side effects¶
Construction establishes the original template source and a separate working package state. Normal callers do not need to call load() immediately after construction.
render()¶
Recommended · Simple Template Processing
Applies values and repeating data previously staged through assign() and assignRepeating() to the current working document. It also executes the classic template-language filters, list placeholders, conditions, and foreach processing.
When to use¶
Use render() for the classic visible template language.
$template->assign([
'customer' => 'Example GmbH',
]);
$template->assignRepeating('items', [
['name' => 'Consulting', 'price' => '900.00'],
]);
$template->render();
$template->save('invoice.odt');
Lifecycle and side effects¶
render() mutates the current working DOM. It does not save the ODT package.
It is not a "rerender from original source" operation. Once a placeholder has been consumed in the working document, a later render() does not implicitly reconstruct it. Use load() when an explicit reset to the original template is required.
Assigning an OdtElement through assign() and then calling render() remains a compatibility path. For new structured content use setElement().
save()¶
Recommended · Template & Document
Finalizes the current working document and writes it as an ODT package.
Parameters¶
| Parameter | Type | Description |
|---|---|---|
$outputPath |
string |
Absolute or relative path for the output ODT file. |
Lifecycle and side effects¶
save() finalizes document-owned graphic/font requirements and package state before writing the file.
It does not call render(). For classic processing the caller must render first.
Structured operations such as setElement() mutate their document structure directly and can be saved without a classic render when no staged classic values need processing.
load()¶
Advanced · Lifecycle
Resets the current working document from the instance's original template source and prepares it again.
When to use¶
Use load() only when you deliberately want to discard working-document mutations and return to the original source of this OdtTemplate instance.
Lifecycle and side effects¶
load() resets working package state, the legacy structured-render lifecycle state, and the successful common Phase-E automation guard.
It is not required after construction.
$template->assign(['name' => 'First']);
$template->render();
$template->load(); // working document returns to the original template
cleanup()¶
Advanced · Lifecycle
Releases the temporary package workspace used by this template instance.
A shutdown cleanup callback is registered by the constructor, so normal short-lived application code does not need to call cleanup() merely to obtain cleanup at process shutdown.
Do not rely on further authoring calls after explicit cleanup; object reuse after cleanup is not part of the characterized public contract.
refresh()¶
Compatibility · Lifecycle
Performs historical pre-reset finalization/persistence work and then resets the working document to the original template source.
Despite its name, refresh() does not mean "reload while preserving current rendered values".
$template->assign(['name' => 'Rendered']);
$template->render();
$template->refresh();
// The working document is back at the original template state.
For new code, use the normal lifecycle directly. If an explicit reset is required, load() expresses that intent more clearly.
Structured insertion in the lifecycle¶
setElement() is the Recommended entry point when PHP owns a structured dynamic region:
It materializes the structured element immediately rather than staging it for render().
use OdtTemplateEngine\Elements\RichText;
$content = new RichText();
$content->addParagraph('Generated structured content');
$template->setElement('content', $content);
$template->save('output.odt');
The full OdtElement, RichText, Paragraph, list, table, image, and frame contracts belong to the Structured Content reference.