發布指定內容類型的內容項目後,會使用內容版面配置編譯器輸出內容版面配置的 HTML。
在進行編譯時,cec compile-content 命令將會在元件的 render.js 檔案所在位置中尋找 compile.js 檔案:
src
components
<yourComponent>
assets
render.jscompile.js如果此檔案不存在,便不會編譯版面配置,但將會在程式實際執行時呈現該版面配置。
如果此檔案存在,則必須實行會傳回承諾的 compile() 介面。例如,下列是一個內容版面配置編譯器,後面接著產生的 layout.html 輸出:
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;
您可以將產生的 layout.html 插入 Web 應用程式,或任何需有內容版面配置靜態 HTML 輸出的位置。layout.html 會輸出至 compile.js 檔案所在的目錄。
{{{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}}