CMS compiler
CMSCOMPILER(), CMSRender and the methods of a compiled page.
On this page
API
method
CMSCOMPILER(html, widgets, [used])
Compiles HTML of a page built with the Total.js CMS editor into a fast renderer.
CMSCOMPILER(html, widgets, [used])| Parameter | Type | Description |
|---|---|---|
html |
String |
The HTML. |
widgets |
Array|Object |
Widgets: { id, css, js, ui: { css, js }, render }. |
used? |
Boolean |
Collects CSS and JS only of the used widgets. Default: false |
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.
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: '/' });
class
CMSRender
The compiled page returned by CMSCOMPILER().
CMSRenderProperties: 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.
var pages = {};
function getpage(id, html) {
return pages[id] || (pages[id] = CMSCOMPILER(html, MAIN.widgets).importcss().importjs());
}
method
cms.render(meta, [layout], [callback])
Renders the compiled page to HTML.
cms.render(meta, [layout], [callback])| Parameter | Type | Description |
|---|---|---|
meta |
Object |
Data for widgets. |
layout? |
CMSRender |
A compiled layout. |
callback? |
Function(err, html) |
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.
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>
});
var html = await page.render({ url: '/', breadcrumb: [{ name: 'Home', url: '/' }], navigation: function(id) {
return MAIN.navigations[id];
}});
method
cms.importcss([value])
Inserts CSS before </head>.
cms.importcss([value])| Parameter | Type | Description |
|---|---|---|
value? |
String |
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.
page.importcss('<link rel="stylesheet" href="/css/default.css" />');
method
cms.importjs([value])
Inserts JavaScript before </body>.
cms.importjs([value])| Parameter | Type | Description |
|---|---|---|
value? |
String |
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.
page.importjs();
method
cms.importmeta(value)
Inserts HTML before </head>.
cms.importmeta(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
HTML to insert. |
Returns CMSRender
Use it for <title> and <meta> tags. Nothing happens when the HTML has no </head>.
page.importmeta('<title>' + page_title.encode() + '</title>');
delegate
widget.render(opt, next)
Renders one widget instance.
widget.render(opt, next)| Parameter | Type | Description |
|---|---|---|
opt |
Object |
The instance data. |
next |
Function(html, [replace], [cache]) |
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.
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);
}};