Inhoudlay-outcompilers worden gebruikt om HTML uit te voeren voor een inhoudlay-out wanneer een inhouditem van een opgegeven inhoudtype wordt gepubliceerd.
Tijdens het compileren wordt er met de opdracht cec compile-content gezocht naar het bestand compile.js op dezelfde locatie als het bestand render.js voor de component:
src
components
<uwComponent>
assets
render.jscompile.jsAls dit bestand niet bestaat, wordt de lay-out niet gecompileerd en wordt deze gerenderd tijdens runtime.
Als het bestand wel bestaat, moet hiervoor de interface compile() worden geïmplementeerd, die een Promise-object (toezeggingsobject) retourneert. Zo is het volgende een inhoudlay-outcompiler, gevolgd door de resulterende layout.html-uitvoer:
var fs = require('fs'),
path = require('path'),
mustache = require('mustache');
var ContentLayout = function (params) {
this.contentClient = params.contentClient;
this.contentItemData = params.contentItemData || {};
this.scsData = params.scsData;
};
ContentLayout.prototype = {
contentVersion: '>=1.0.0 <2.0.0',
compile: function () {
var compiledContent = '',
content = JSON.parse(JSON.stringify(this.contentItemData)),
contentClient = this.contentClient;
// Store the id
content.fields.author_id = content.id;
if (this.scsData) {
content.scsData = this.scsData;
}
try {
// add in style - possibly add to head but inline for simplicity
var templateStyle = fs.readFileSync(path.join(__dirname, 'design.css'), 'utf8');
content.style = '<style>' + templateStyle + '</style>';
var templateHtml = fs.readFileSync(path.join(__dirname, 'layout.html'), 'utf8');
compiledContent = mustache.render(templateHtml, content);
} catch (e) {
console.error(e.stack);
}
return Promise.resolve({
content: compiledContent,
hydrate: true
});
}
};
module.exports = ContentLayout;
De resulterende layout.html kan in een webapplicatie worden ingevoegd of overal waar u statische HTML-uitvoer van de inhoudlay-out nodig hebt. De layout.html wordt uitgevoerd naar dezelfde directory als het compile.js-bestand.
{{{style}}}
{{#fields}}
<div class="author-container">
<span class="author-name" onclick='{{scsData.contentTriggerFunction}}("starter-blog-post_author eq \"{{author_id}}\"")'>{{starter-blog-author_name}}</span>
{{/fields}}