Search Docs
Loading...
Skip to content

Class: VariableAPI

Manage text variables within design templates.

Text variables enable dynamic content replacement in design templates. Variables are stored as key-value pairs and can be referenced in text blocks for automated content updates.

// Configure a text block that displays 'Hello, World'
const block = engine.block.create('text');
engine.block.setText(block, 'Hello, {{name}}!');
engine.variable.setString('name', 'World');

Color, number, image and typeface variables hold typed values. A block property bound to one of them with engine.block.setVariableBinding follows every change of the variable.

engine.variable.setColor('brand', { r: 1, g: 0, b: 0, a: 1 });
engine.block.setVariableBinding(fill, 'fill/color/value', 'brand');

The scene saves its variables, and undo reverts their changes. A variable set while no scene exists waits for the next scene: that scene takes it when it is created or loaded, and its value replaces the loaded one.

A value set holds alternative values for some variables, such as worst-case texts or a client’s copy. The active set wins over the default values. A call without a target uses the layer that the scene shows, so getString returns what setString wrote.

const longNames = engine.variable.createValueSet('Long names');
engine.variable.setString('name', 'Maximiliane', { target: { valueSet: longNames } });
engine.variable.setActiveValueSet(longNames);
engine.variable.getString('name'); // 'Maximiliane'
engine.variable.getString('name', { target: 'Default' }); // 'World'

Implements#

  • IVariableAPI

Constructors#

Constructor#


| Parameter | Type | | —— | —— | | engine | EngineBindingHandle |

Returns#

VariableAPI

Methods#

findAll()#


Get all text variable names of the scene, or of the variables that wait for a scene.

Returns#

string[]

List of variable names.

Implementation of#

IVariableAPI.findAll

Signature#

findAll(): string[]

setString()#


Set a text variable’s value. Creates the variable if it doesn’t exist. The scene saves its variables, and undo reverts their changes. A variable set while no scene exists waits for the next scene. That scene takes it when it is created or loaded, as this setter without a target writes it. A value of another type than the loaded variable is dropped. A new key must not be empty, start or end with whitespace, contain {{ or }}, or be reserved: scene_root and keys that start with ubq..

Parameters#

Parameter Type Description
key string The variable’s key.
value string The text value to assign.
options? VariableOptions The layer to write. By default the active value set when it holds the key, else the default value.

Returns#

void

Implementation of#

IVariableAPI.setString

Signature#

setString(key: string, value: string, options?: VariableOptions): void

getString()#


Get a text variable’s value.

Parameters#

Parameter Type Description
key string The variable’s key.
options? VariableOptions The layer to read. By default the value that the scene shows.

Returns#

string

The text value of the variable.

Implementation of#

IVariableAPI.getString

Signature#

getString(key: string, options?: VariableOptions): string

remove()#


Remove a variable of any type from the engine.

Bound properties keep their last value and their binding, so direct writes to them still fail with BLOCK.PROPERTY_BOUND. Setting a variable with the same key and type again makes them follow it again.

Parameters#

Parameter Type Description
key string The variable’s key to remove.
options? VariableOptions The layer to remove from. By default, and with the target 'Default', the variable goes with its value in every value set. A value set target removes only the value of that set.

Returns#

void

Implementation of#

IVariableAPI.remove

Signature#

remove(key: string, options?: VariableOptions): void

createValueSet()#


Create an empty value set in the scene. A value set holds alternative values for some scene variables; write them with the target { valueSet: id }. The scene saves its sets, and undo reverts their changes.

Parameters#

Parameter Type Description
name string The name of the set. It must be unique among the sets of the scene, must not be empty, and must not start or end with whitespace.

Returns#

string

The id of the new set. The scene saves it, so it stays the same across saves and loads.

Implementation of#

IVariableAPI.createValueSet

Signature#

createValueSet(name: string): string

destroyValueSet()#


Destroy a value set and its values. When the set is active, no set is active afterwards.

Parameters#

Parameter Type Description
set string The id of the set.

Returns#

void

Implementation of#

IVariableAPI.destroyValueSet

Signature#

destroyValueSet(set: string): void

setValueSetName()#


Rename a value set. The id stays, and an active set stays active.

Parameters#

Parameter Type Description
set string The id of the set.
name string The new name, valid as for createValueSet.

Returns#

void

Implementation of#

IVariableAPI.setValueSetName

Signature#

setValueSetName(set: string, name: string): void

getValueSetName()#


Get the name of a value set.

Parameters#

Parameter Type Description
set string The id of the set.

Returns#

string

The name of the set.

Implementation of#

IVariableAPI.getValueSetName

Signature#

getValueSetName(set: string): string

findAllValueSets()#


Get the value sets of the scene.

Returns#

string[]

The ids of the sets in creation order, empty when no scene exists.

Implementation of#

IVariableAPI.findAllValueSets

Signature#

findAllValueSets(): string[]

findValueSetByName()#


Find a value set by its name.

Parameters#

Parameter Type Description
name string The name of the set.

Returns#

string

The id, or null when no set of the scene has this name or no scene exists.

Implementation of#

IVariableAPI.findValueSetByName

Signature#

findValueSetByName(name: string): string

setActiveValueSet()#


Activate a value set, or no set. The values of the active set win over the default values in text, bound properties and getters. Exports show the active set, as the canvas does. A scene has one active set and saves it. Undo reverts the change.

Parameters#

Parameter Type Description
set string The id of the set, or null to show the default values.

Returns#

void

Implementation of#

IVariableAPI.setActiveValueSet

Signature#

setActiveValueSet(set: string): void

getActiveValueSet()#


Get the active value set.

Returns#

string

The id of the active set, or null when no set is active or no scene exists.

Implementation of#

IVariableAPI.getActiveValueSet

Signature#

getActiveValueSet(): string

rename()#


Rename a variable of any type. The value, the type and the values in every value set move to the new key. Every block property bound to the variable follows the new key and keeps its value. For a text variable, every {{key}} token in text blocks and page titles is rewritten too. A key inside a longer expression or a statement, such as {% if key %}, is not renamed.

Parameters#

Parameter Type Description
from string The current key of the variable.
to string The new key. It must not be empty, start or end with whitespace, contain {{ or }}, name another variable, or be reserved: scene_root and keys that start with ubq..

Returns#

void

Implementation of#

IVariableAPI.rename

Signature#

rename(from: string, to: string): void

setColor()#


Set a color variable. Creates the variable if it doesn’t exist. Every block property bound to the variable takes the color that the scene shows. If a bound property rejects the value, the call throws its error, but the variable and the other bound properties change anyway. A new key must not be empty, start or end with whitespace, contain {{ or }}, or be reserved: scene_root and keys that start with ubq..

Parameters#

Parameter Type Description
key string The variable’s key.
value Color The color to assign.
options? VariableOptions The layer to write. By default the active value set when it holds the key, else the default value.

Returns#

void

Implementation of#

IVariableAPI.setColor

Signature#

setColor(key: string, value: Color, options?: VariableOptions): void

getColor()#


Get a color variable’s value.

Parameters#

Parameter Type Description
key string The variable’s key.
options? VariableOptions The layer to read. By default the value that the scene shows.

Returns#

Color

The color value of the variable.

Implementation of#

IVariableAPI.getColor

Signature#

getColor(key: string, options?: VariableOptions): Color

setNumber()#


Set a number variable. Creates the variable if it doesn’t exist. Every block property bound to the variable takes the number that the scene shows. If a bound property rejects the value, the call throws its error, but the variable and the other bound properties change anyway. A new key must not be empty, start or end with whitespace, contain {{ or }}, or be reserved: scene_root and keys that start with ubq..

Parameters#

Parameter Type Description
key string The variable’s key.
value number The finite number to assign.
options? VariableOptions The layer to write. By default the active value set when it holds the key, else the default value.

Returns#

void

Implementation of#

IVariableAPI.setNumber

Signature#

setNumber(key: string, value: number, options?: VariableOptions): void

getNumber()#


Get a number variable’s value.

Parameters#

Parameter Type Description
key string The variable’s key.
options? VariableOptions The layer to read. By default the value that the scene shows.

Returns#

number

The number value of the variable.

Implementation of#

IVariableAPI.getNumber

Signature#

getNumber(key: string, options?: VariableOptions): number

setImage()#


Set an image variable. Creates the variable if it doesn’t exist. Every block property bound to the variable takes the URI that the scene shows. If a bound property rejects the value, the call throws its error, but the variable and the other bound properties change anyway. A new key must not be empty, start or end with whitespace, contain {{ or }}, or be reserved: scene_root and keys that start with ubq..

Parameters#

Parameter Type Description
key string The variable’s key.
uri string The URI of the image.
options? VariableOptions The layer to write. By default the active value set when it holds the key, else the default value.

Returns#

void

Implementation of#

IVariableAPI.setImage

Signature#

setImage(key: string, uri: string, options?: VariableOptions): void

getImage()#


Get an image variable’s URI.

Parameters#

Parameter Type Description
key string The variable’s key.
options? VariableOptions The layer to read. By default the value that the scene shows.

Returns#

string

The URI of the image.

Implementation of#

IVariableAPI.getImage

Signature#

getImage(key: string, options?: VariableOptions): string

setTypeface()#


Set a typeface variable. Creates the variable if it doesn’t exist. Every text block whose typeface is bound to the variable takes the typeface that the scene shows, over its whole text. If a bound property rejects the value, the call throws its error, but the variable and the other bound properties change anyway. A new key must not be empty, start or end with whitespace, contain {{ or }}, or be reserved: scene_root and keys that start with ubq..

Parameters#

Parameter Type Description
key string The variable’s key.
typeface Typeface The typeface. It needs a name and at least one font.
options? VariableOptions The layer to write. By default the active value set when it holds the key, else the default value.

Returns#

void

Implementation of#

IVariableAPI.setTypeface

Signature#

setTypeface(key: string, typeface: Typeface, options?: VariableOptions): void

getTypeface()#


Get a typeface variable’s value.

Parameters#

Parameter Type Description
key string The variable’s key.
options? VariableOptions The layer to read. By default the value that the scene shows.

Returns#

Typeface

The typeface of the variable.

Implementation of#

IVariableAPI.getTypeface

Signature#

getTypeface(key: string, options?: VariableOptions): Typeface

getType()#


Get the type of a variable.

Parameters#

Parameter Type Description
key string The variable’s key.
options? VariableOptions The layer to read. By default the value that the scene shows.

Returns#

VariableType

The variable’s type.

Implementation of#

IVariableAPI.getType

Signature#

getType(key: string, options?: VariableOptions): VariableType

findAllOfType()#


Get the names of all variables of one type, of the scene or of the variables that wait for a scene. findAll returns only text variables.

Parameters#

Parameter Type Description
type VariableType The type of the variables.

Returns#

string[]

List of variable names.

Implementation of#

IVariableAPI.findAllOfType

Signature#

findAllOfType(type: VariableType): string[]