Widget file reference
What a widget declares in its server script.
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.
exports.id = 'greeting';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.
exports.name = 'Greeting';A widget without a name is not installed.
property
exports.author = 'Jane Doe';
The author of the widget.
exports.author = 'Jane Doe';
property
exports.version = '1';
The version of the widget, as text.
exports.version = '1';
property
exports.preview = 'data:image/png;base64,...';
The picture of the widget in the admin.
exports.preview = 'data:image/png;base64,...';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.
exports.config = { name: 'World', align: 'left' };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.
exports.dependencies = ['https://example.com/lib.css', 'https://example.com/lib.js'];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.
exports.cache = 'url';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.
exports.render = function(opt, next) {}| Parameter | Type | Description |
|---|---|---|
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.
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.
exports.install = function(db) {}| Parameter | Type | Description |
|---|---|---|
db |
Object |
The CMS data, with vars, config and storage. It is also this. |
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.
exports.uninstall = function(db) {}| Parameter | Type | Description |
|---|---|---|
db |
Object |
The CMS data. It is also this. |