---
title: CMS compiler
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/cms/
updated: 2026-11-18T16:57:44.579Z
---

# CMS compiler

> CMSCOMPILER(), CMSRender and the methods of a compiled page.

## API

### CMSCOMPILER(html, widgets, [used])

Type: `method`

Compiles HTML of a page built with the Total.js CMS editor into a fast renderer.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | The HTML. |
| `widgets` | `Array|Object` | yes |  | Widgets: `{ id, css, js, ui: { css, js }, render }`. |
| `used` | `Boolean` | no | `false` | Collects CSS and JS only of the used widgets. |

Returns: `CMSRender` The compiled page.

Widgets are elements with `data-cms="ID__CONFIG"`, where `CONFIG` is URL-encoded JSON. A widget without `render` is kept as static HTML; a widget that is not found is removed. Elements with the class `CMS_trash` or `CMS_editor` are removed, `CMS_unwrap` elements are replaced by their content, and other `CMS_` classes are cleaned (except `CMS_hidden`, `CMS_mv`, `CMS_mh`, `CMS_expression`, `CMS_multiple`, `CMS_keyword` and `CMS_monospace`). `<script type="text/navigation" name="...">` and `<script type="text/breadcrumb">` contain Tangular templates. The element with `id="CMS"` marks where a page goes when the result is used as a layout.

Without `used`, the CSS and JS of all widgets go to the result; with `used` only of the widgets in the HTML. The `widgets` object form (keyed by id) works only with `used`.

Example: Compile and render
```js
var widgets = [{ id: 'hello', css: '.hello{color:red}', render: function(opt, next) {
	next('<b>Hello ' + opt.config.name + '</b>');
}}];

var page = CMSCOMPILER(html, widgets);
page.importcss().importjs();

var output = await page.render({ url: '/' });
```

### CMSRender

Type: `class`

The compiled page returned by `CMSCOMPILER()`.

Properties: `css` and `js` (the minified code of widgets as strings), `widgets` (compiled widget instances), `tangular` (navigation and breadcrumb templates), `expressions` and `multiple` (elements with the `CMS_expression` and `CMS_multiple` classes) and `cache` (output of widgets cached with `next(html, replace, true)`). Keep the instance in memory and call `render()` for each request.

Example: Cache the compiled page
```js
var pages = {};

function getpage(id, html) {
	return pages[id] || (pages[id] = CMSCOMPILER(html, MAIN.widgets).importcss().importjs());
}
```

### cms.render(meta, [layout], [callback])

Type: `method`

Renders the compiled page to HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `meta` | `Object` | yes |  | Data for widgets. |
| `layout` | `CMSRender` | no |  | A compiled layout. |
| `callback` | `Function(err, html)` | no |  | Receives the HTML. |

Returns: `Promise` Only without a callback.

Calls `render` of each widget in order, then the navigation and breadcrumb templates. `meta` is passed to widgets; the framework reads `meta.url`, `meta.body` (the content for a layout), `meta.navigation` (a `function(id)` returning the items of a navigation), `meta.breadcrumb` (an array), `meta.widgets` (widgets that replace the compiled ones by id; `cache: 'url'` caches per URL) and `meta.inlinecache` (an object that caches the rendered navigation). With `layout` (another `CMSRender`) the page is inserted into the layout. Without a callback it returns a Promise.

Example: Page in a layout
```js
var layout = CMSCOMPILER('<html><body><main id="CMS"></main></body></html>', []);
var page = CMSCOMPILER('<p>Page</p>', []);

page.render({ url: '/' }, layout, function(err, html) {
	// <html><body><main id="CMS"><p>Page</p></main></body></html>
});
```

Example: Navigation
```js
var html = await page.render({ url: '/', breadcrumb: [{ name: 'Home', url: '/' }], navigation: function(id) {
	return MAIN.navigations[id];
}});
```

### cms.importcss([value])

Type: `method`

Inserts CSS before `</head>`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | no |  | HTML to insert. |

Returns: `CMSRender`

Without `value` it inserts `<style>` with the CSS of widgets (nothing when there is none). Without `</head>` the value is added to the beginning.

Example: Own stylesheet
```js
page.importcss('<link rel="stylesheet" href="/css/default.css" />');
```

### cms.importjs([value])

Type: `method`

Inserts JavaScript before `</body>`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | no |  | HTML to insert. |

Returns: `CMSRender`

Without `value` it inserts `<script>` with the JS of widgets (nothing when there is none). Without `</body>` the value is added to the end.

Example: Widget scripts
```js
page.importjs();
```

### cms.importmeta(value)

Type: `method`

Inserts HTML before `</head>`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | HTML to insert. |

Returns: `CMSRender`

Use it for `<title>` and `<meta>` tags. Nothing happens when the HTML has no `</head>`.

Example: Title
```js
page.importmeta('<title>' + page_title.encode() + '</title>');
```

### widget.render(opt, next)

Type: `delegate`

Renders one widget instance.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `opt` | `Object` | yes |  | The instance data. |
| `next` | `Function(html, [replace], [cache])` | yes |  | Returns the output. |

`opt` contains the keys of `meta` and `id` (the instance id), `widget` (the widget id), `config` (the parsed configuration), `body` (the cleaned HTML of the instance), `html` (its inner HTML) and `version`. Call `next(html, [replace], [cache])`: without `replace` the HTML goes inside the widget element; with `replace` it replaces the whole element. With `cache` the output is cached in the compiled page.

Example: Widget with cache
```js
var widget = { id: 'news', render: async function(opt, next) {
	var items = await DATA.find('nosql/news').take(opt.config.count || 5).promise();
	next(items.map(n => '<li>' + n.name.encode() + '</li>').join(''), false, true);
}};
```
