---
title: CMS compiler
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/cms/
updated: 2026-11-19T16:17:02.153Z
---

# CMS compiler

> F.TCMS.compile(), the methods of a compiled page, F.TCMS.widgets() and F.TCMS.run().

## API

### F.TCMS.compile(html, widgets, [used])

Type: `method`

Compiles a page of the Total.js CMS into a render object.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | HTML of the page. |
| `widgets` | `Array|Object` | yes |  | Widgets as an array with `id`, or an object by id. A widget has `render(opt, next)` and optional `css`, `js` and `ui`. |
| `used` | `Boolean` | no | `false` | Joins CSS and JS only of the widgets used on the page. |

Returns: `CMSRender` Compiled page with `render()`, `importcss()`, `importjs()`, `importmeta()`, `css`, `js` and `widgets`.

Finds the widgets (elements with `data-cms="<id>__<config>"`, where config is URL-encoded JSON) and looks them up in `widgets` by `id`. Elements of unknown widgets are removed, widgets without `render` stay as static HTML. `<script type="text/navigation" id="...">` and `<script type="text/breadcrumb">` become Tangular templates; the `id` of a navigation is its `id` attribute, or a hash of its `name` attribute when it has no `id`. Elements with the class `CMS_trash` or `CMS_editor` are removed. An element with `id="CMS"` marks the place for the body when the page is used as a layout.

The CSS and JS of the widgets (`widget.css`, `widget.js`, and `widget.ui.css`, `widget.ui.js` from `F.TCMS.run()`) are minified into `page.css` and `page.js`, for all widgets or, with `used: true`, only for the widgets on the page.

Example: Compile
```js
var page = F.TCMS.compile(item.html, MAIN.widgets, true);
page.importcss().importjs();
```

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

Type: `method`

Renders a compiled CMS page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `meta` | `Object` | yes |  | Data for the widgets and templates, see the table. |
| `layout` | `CMSRender` | no |  | A compiled layout page. |
| `callback` | `Function` | no |  | `function(err, html)`. Without it a promise is returned. |

Returns: `Promise` With the HTML when no callback is given.

Calls `render(opt, next)` of every widget on the page, one after another, and fills the navigation and breadcrumb templates. `opt` contains all keys of `meta` (except `widgets`) and `id` (the `data-cms-id` of the element), `widget` (widget id), `config`, `html` (the inner HTML from the editor), `body`, `template` (the `<template>` of a widget from `F.TCMS.run()`, or an empty string) and `cacheid`.

The widget answers with `next(html, [replace], [cache])`: the HTML is put inside the widget element, or replaces the element with `replace: true`; `cache: true` keeps the output in the page for the next renders (a widget in `meta.widgets` with `cache: 'url'` caches per URL).

| `meta` key | Description |
| --- | --- |
| `url` | URL of the request |
| `navigation` | `function(id)` that returns the value of a navigation template |
| `breadcrumb` | Array for the breadcrumb template |
| `widgets` | Widgets that override the `render` of the compiled ones |
| `body` | Content for the `id="CMS"` element of a layout |
| `inlinecache` | Object that stores the rendered navigations and breadcrumbs |

With `layout` (another compiled page) the output goes into `meta.body` and the layout is rendered.

Example: In a route
```js
async function page($) {
	var html = await MAIN.page.render({ url: $.url, controller: $, navigation: id => MAIN.navigations[id] });
	$.html(html);
}
```

### cmsrender.importcss([html])

Type: `method`

Inserts CSS before `</head>`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | no | `'<style>' + page.css + '</style>'` | HTML to insert. |

Returns: `CMSRender` The page.

Without an argument it inserts `<style>` with `page.css` (nothing when it is empty). When the page has no `</head>`, the CSS is appended to the end of the first static HTML part of the page: before the first widget when there is HTML before it, at the end of the page when the page has no widgets.

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

### cmsrender.importjs([html])

Type: `method`

Inserts JavaScript before `</body>`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | no | `'<script>' + page.js + '</script>'` | HTML to insert. |

Returns: `CMSRender` The page.

Without an argument it inserts `<script>` with `page.js` (nothing when it is empty). When the page has no `</body>`, the script is added at the end.

Example: Inline
```js
page.importjs();
```

### cmsrender.importmeta(html)

Type: `method`

Inserts HTML before `</head>`.

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

Returns: `CMSRender` The page.

For meta tags, a title or other head content. Nothing happens when the page has no `</head>`.

Example: Meta
```js
page.importmeta('<meta name="description" content="' + item.description.encode() + '">');
```

### F.TCMS.widgets(html)

Type: `method`

Lists the widgets used in a CMS page.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | HTML of the page. |

Returns: `Array` Widget elements.

Returns an array with `id` (widget id), `uid` (`data-cms-id`), `config` (parsed JSON), `html` (inner HTML), `beg` and `end` (opening and closing tag) and `indexer` for every widget element. Useful to find which widgets a page needs.

Example: Used widgets
```js
var ids = F.TCMS.widgets(item.html).map(w => w.id);
```

### F.TCMS.run(html)

Type: `method`

Creates a widget from its HTML source.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `html` | `String` | yes |  | Widget source. |

Returns: `Object` The widget.

A widget source has the parts `<script total>` (server code that fills `exports`, such as `exports.id`, `exports.name` and `exports.render`), `<style>`, `<script>`, `<body>`, `<template>`, `<settings>` and `<readme>`. The word `CLASS` in the style, settings, template, script and body parts is replaced with `w-` and the id as a slug without dashes (`My Widget` gives `w-mywidget`). The returned object is the `exports` with `ui` (the parts, plus `cls` and `editor`), `id` (the name when missing), `dependencies` turned into `<script>`/`<link>` tags and `uninstall`. Pass such objects to `F.TCMS.compile()`.

Example: Load widgets
```js
var widgets = sources.map(source => F.TCMS.run(source));
var page = F.TCMS.compile(html, widgets);
```
