Spanish Total.js
English
Documentation For AI agents
Contents

Widget file reference

What a widget declares in its server script.

.md GitHub
On this page

The server script is the <script total> section of the widget. CMS runs it when it installs, edits or loads the widget, and reads these properties and functions from exports.

Entries of type property are values that you set. Entries of type event are functions that you define and CMS calls.

API

property

exports.id = 'greeting';

The identifier of the widget.

Returns String

Pages refer to the widget by this value, and CLASS becomes w- plus the identifier without dashes. A widget saved with the identifier of an installed widget replaces it. Without id, CMS uses name as the identifier.

property

exports.name = 'Greeting';

The name shown in the admin and the widget picker.

A widget without a name is not installed.

property

exports.author = 'Jane Doe';

The author of the widget.
property

exports.version = '1';

The version of the widget, as text.
property

exports.preview = 'data:image/png;base64,...';

The picture of the widget in the admin.

A data: URI or an address. CMS downloads an https:// picture into its files when it saves the widget. See Styles, scripts and assets.

property

exports.config = { name: 'World', align: 'left' };

The default configuration of a new instance.

The editor copies it into each instance that the user adds. See Settings and their form.

property

exports.dependencies = ['https://example.com/lib.css', 'https://example.com/lib.js'];

Files that CMS adds to the head of every public page.

An array or a string separated by commas or semicolons. An address that ends with .css becomes a <link>, one that ends with .js becomes a <script>, and a value with < is added as it is.

property

exports.cache = 'url';

Keeps one cached result for each address.

With the value url, a cached render result is kept separately for each page address. It matters for widgets in a layout. See Rendering on the public site.

event

exports.render = function(opt, next) {}

Makes the content of an instance on the public site.
ParameterTypeDescription
opt Object The instance and the request. See Render reference.
next Function next(content, [replace], [cache]). See Render reference.

Called for each instance each time the page is built, unless a cached result exists. Not called in the editor.

Add lazy loading to the images of the instance
exports.render = function(opt, next) {
	next(opt.html.replace(/<img\s/g, '<img loading="lazy" '), false, true);
};
event

exports.install = function(db) {}

Runs when CMS installs, updates or loads the widget.
ParameterTypeDescription
db Object The CMS data, with vars, config and storage. It is also this.
Create a variable with a default value
exports.install = function(db) {
	if (!db.vars.supportemail)
		db.vars.supportemail = 'help@example.com';
};
event

exports.uninstall = function(db) {}

Runs when the widget is removed and before it is updated.
ParameterTypeDescription
db Object The CMS data. It is also this.
Made in Europe