Inhaltslayout-Compiler geben HTML für ein Inhaltslayout aus, wenn ein Inhaltselement eines angegebenen Inhaltstyps veröffentlicht wird.
Bei der Kompilierung sucht der Befehl cec compile-content
nach der Datei compile.js
in demselben Speicherort, in dem auch die Datei render.js
für die Komponente gespeichert ist:
src
components
<yourComponent>
assets
render.js
compile.js
Wenn diese Datei nicht vorhanden ist, ist das Layout nicht kompiliert und wird zur Laufzeit gerendert.
Wenn die Datei vorhanden ist, muss sie eine compile()
-Schnittstelle implementieren, die einen Promise zurückgibt. Beispiel: Im Folgenden sehen Sie einen Inhaltslayout-Compiler gefolgt von der resultierenden layout.html-Ausgabe:
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;
Die resultierende layout.html kann in eine Webanwendung oder in jeden anderen Ort eingefügt werden, an dem Sie eine statische HTML-Ausgabe des Inhaltslayouts benötigen. layout.html wird an dasselbe Verzeichnis wie die Datei compile.js ausgegeben.
{{{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}}