[英]Render def file independently in dot.js
我使用{{#def.header}}代碼段在index.dot中包含一個header.def文件,並使用以下代碼段呈現index.dot文件。
var dotjs = require('dot');
var dots = dotjs.process({ path: "./views"});
app.get('/',function(request, response){
response.send(dots.index());
});
我還希望做的是僅在單獨的URL上呈現標題。 就像是:
app.get('/header',function(request, response){
response.send(dots.header()); // header.def is not compiled to a dot.header function
});
也許您可以創建另一個.def文件(例如“ onlyHead.dot”),並將靜態頭文件包含在該文件頭中,而無需其他操作。 然后以與您相同的方式發送onlyHead.dot。
您還可以使用模板引擎執行以下操作:
在主應用程序中:
/* Template = doT*/
var dot = require("dot").process({
path: (__dirname + "/views")
});
// view engine setup using .dot files
app.engine('dot', function(template, options, cb){
return cb(null, dot[path.parse(template).name](options));
});
然后在您的視圖中,可以這樣渲染它(使用router.get或通過app.get渲染):
app.get('/',function(request, response){
var myData = {
title : 'My title',
someData : 'My Data'}
response.render('index', myData);
});
然后,您的index.dot模板可以如下所示:
{{#def.static}}
<h1>{{=it.title}}</h1>
<div>The requested data is: {{=it.someData}} </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.