CMS compiler
F.TCMS.compile(), the methods of a compiled page, F.TCMS.widgets() and F.TCMS.run().
On this page
API
method
F.TCMS.compile(html, widgets, [used])
Compiles a page of the Total.js CMS into a render object.
F.TCMS.compile(html, widgets, [used])| Parameter | Type | Description |
|---|---|---|
html |
String |
HTML of the page. |
widgets |
Array|Object |
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 |
Joins CSS and JS only of the widgets used on the page. Default: false |
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.
var page = F.TCMS.compile(item.html, MAIN.widgets, true);
page.importcss().importjs();
method
cmsrender.render(meta, [layout], [callback])
Renders a compiled CMS page.
cmsrender.render(meta, [layout], [callback])| Parameter | Type | Description |
|---|---|---|
meta |
Object |
Data for the widgets and templates, see the table. |
layout? |
CMSRender |
A compiled layout page. |
callback? |
Function |
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.
async function page($) {
var html = await MAIN.page.render({ url: $.url, controller: $, navigation: id => MAIN.navigations[id] });
$.html(html);
}
method
cmsrender.importcss([html])
Inserts CSS before </head>.
cmsrender.importcss([html])| Parameter | Type | Description |
|---|---|---|
html? |
String |
HTML to insert. Default: '<style>' + page.css + '</style>' |
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.
page.importcss('<link rel="stylesheet" href="/css/page.css">');
method
cmsrender.importjs([html])
Inserts JavaScript before </body>.
cmsrender.importjs([html])| Parameter | Type | Description |
|---|---|---|
html? |
String |
HTML to insert. Default: '<script>' + page.js + '</script>' |
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.
page.importjs();
method
cmsrender.importmeta(html)
Inserts HTML before </head>.
cmsrender.importmeta(html)| Parameter | Type | Description |
|---|---|---|
html |
String |
HTML to insert. |
Returns CMSRender The page.
For meta tags, a title or other head content. Nothing happens when the page has no </head>.
page.importmeta('<meta name="description" content="' + item.description.encode() + '">');
method
F.TCMS.widgets(html)
Lists the widgets used in a CMS page.
F.TCMS.widgets(html)| Parameter | Type | Description |
|---|---|---|
html |
String |
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.
var ids = F.TCMS.widgets(item.html).map(w => w.id);
method
F.TCMS.run(html)
Creates a widget from its HTML source.
F.TCMS.run(html)| Parameter | Type | Description |
|---|---|---|
html |
String |
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().
var widgets = sources.map(source => F.TCMS.run(source));
var page = F.TCMS.compile(html, widgets);