Rendering on the public site
Make the content of a widget on the server with render, its template, variables and cache.
Without a server function, the public page shows the HTML that the user saved in the editor. Add exports.render to <script total> when the content must be made each time the page is built, for example from data of the page, the visitor or another service.
render runs only on the public site. The editor always shows the <body> of the widget, so write a <body> that tells the user what the widget will show.
An example
Page info shows the title and address of the current page, the kind of device of the visitor, a variable and the time when it was made. Its HTML comes from the <template> section:
<script total>
exports.id = 'pageinfo';
exports.name = 'Page info';
exports.author = 'Jane Doe';
exports.version = '1';
exports.config = { label: 'You are reading' };
exports.render = function(opt, next) {
var model = {};
model.label = opt.config.label;
model.title = opt.page.title || opt.page.name;
model.url = opt.url;
model.device = opt.mobile ? 'a phone' : 'a computer';
model.company = opt.vars.company || '';
model.time = new Date().format('HH:mm:ss');
next(Tangular.render(opt.template, model));
};
</script>
<style>
.CLASS-box { padding: 15px 20px; background-color: #F0F7F3; border-radius: var(--radius); }
.CLASS-time { color: #888; font-size: 12px; }
</style>
<settings>
<div class="padding">
<ui-component name="input" path="?.label" config="required:1">Label</ui-component>
</div>
<script>
exports.save = function(config, el) {
el.find('.CLASS-label').text(config.label);
};
</script>
</settings>
<template>
<div class="CLASS-box">{{ label }} <b>{{ title }}</b> ({{ url }}) on {{ device }}. {{ company }} <span class="CLASS-time">{{ time }}</span></div>
</template>
<body>
<div class="CLASS-box"><span class="CLASS-label">You are reading</span> the page title (shown on the public site)</div>
</body>On a page named Demo custom, the visitor sees:
<div class="w-pageinfo CMS_mv CMS_mh" data-cms-id="mwnhggal"> <div class="w-pageinfo-box">You are reading <b>Demo custom</b> (/demo-custom/) on a computer. Demo Company <span class="w-pageinfo-time">11:45:34</span></div></div>
What render receives
opt holds the configuration of the instance, its HTML, the template and the context of the request: the page, its address, the variables, the visitor and the controller. next takes the new content. All properties are in the Render reference.
Tangular.render(template, model) fills a Tangular template, and {{ value }} encodes the value as HTML. When you build HTML as text, encode what comes from outside with .encode().
Call next
Call next(content, replace, cache) once, also when something fails:
next(html)puts the content inside the wrapper of the instance. The wrapper keeps its class, margins and the hide options.next(html, true)replaces the whole wrapper. Custom HTML code does this to place the code as it is. Withreplace, an empty content removes the instance from the page.next(html, false, true)keeps the result in a cache.
exports.render = function(opt, next) {
if (!opt.config.url)
return next('');
next('<ui-import config="url:{0}"></ui-import>'.format(opt.config.url), true, true);
};CMS replaces CLASS in <template> before render reads it, but not in the server script or in the HTML that render returns. Write class names there in full, such as w-pageinfo-box.
Variables
After all widgets are rendered, CMS replaces every $key in the page with the value of the variable, also in the output of render. You can also read the values in opt.vars.
Cache
With cache set to true, CMS keeps the result for each instance and calls render again only after it builds the page again. It builds a page again when you save that page (for a widget in a layout, that layout) or any widget, and at the latest every two minutes.
A widget in a layout appears on many pages, but it is one instance, so by default it keeps one result for all of them. Add exports.cache = 'url' to keep one result for each address:
exports.cache = 'url';
exports.render = function(opt, next) {
next('<span>' + opt.url + '</span>', false, true);
};Leave the cache off for content that depends on the visitor, such as opt.user or opt.mobile.