如何在nodejs中渲染doT.js模板?

发布于 2025-01-03 17:25:35 字数 1234 浏览 1 评论 0原文

您好,我想知道如何在 dot.js 模板引擎中渲染输出。我认为这是一个关于 Nodejs 模板的通用问题。(阅读评论以获取更多信息)。我选择这个模板引擎而不是 jade 或 ejs 的原因是因为它似乎是最快的引擎。

这是我的 app.js:

var express = require('express'),
    app = express.createServer(),
    doT = require('doT'),
    pub = __dirname + '/public',
    view =  __dirname + '/views';

app.configure(function(){
    app.set('views', view);
    app.set('view options', {layout: false});
    app.set('view engine', 'dot');
    app.use(app.router);
});

app.register('.html', {
    compile: function(str, opts){
        return function(locals){
            return str;
        }
    }
});


app.get('/', function(req, res){

    //This is where I am trying to send data to the front end....
    res.render('index.html', { output: 'someStuff' });

});

这是我的 html:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>

//This is where I am trying to receive data and output it...
{{=it.output}}

</body>
</html>

我只是找不到关于它的好的文档。这还不够:http://olado.github.com/doT/。如果可以的话请帮忙。这将成倍地提高我对数据如何传递到 Nodejs 中的视图的理解。谢谢。

Hi I would like to know how can I render output in dot.js templating engine. I think it's a generic question about nodejs templating.(read comments for more info). The reason why I chose this template engine instead of jade or ejs is because it seems the fastest engine around.

Here is my app.js:

var express = require('express'),
    app = express.createServer(),
    doT = require('doT'),
    pub = __dirname + '/public',
    view =  __dirname + '/views';

app.configure(function(){
    app.set('views', view);
    app.set('view options', {layout: false});
    app.set('view engine', 'dot');
    app.use(app.router);
});

app.register('.html', {
    compile: function(str, opts){
        return function(locals){
            return str;
        }
    }
});


app.get('/', function(req, res){

    //This is where I am trying to send data to the front end....
    res.render('index.html', { output: 'someStuff' });

});

Here is my html:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>

//This is where I am trying to receive data and output it...
{{=it.output}}

</body>
</html>

I just could not find good docs on it. This was not enough: http://olado.github.com/doT/. Please help, if you can. This will improve my understanding exponentially of how data is passed to the view in nodejs. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

摇划花蜜的午后 2025-01-10 17:25:35

您需要让 Express 知道使用 doT 作为模板引擎,如下所示:

app.set("view engine", "html");
app.register('.html', doT);

You need to let express know to use doT as the template engine like this:

app.set("view engine", "html");
app.register('.html', doT);
落在眉间の轻吻 2025-01-10 17:25:35

我的帖子是一个无耻的插件,但它可能会帮助某人。

我对现有模块与 Express 3.x 的配合方式不太满意,我编写了一个名为 dot-emc 的模块:

https:// github.com/nerdo/dot-emc

用法与上面发布的类似。使用 nom: 安装它,

npm install dot-emc

然后将其设置为默认视图引擎。我更喜欢使用 .def 扩展名,因为我的文本编辑器将 .dot 文件识别为 Graphviz 文件,因此语法略有不同:

app.engine("def", require("dot-emc").__express);
app.set("view engine", "def");

然后您可以像在路线中使用任何其他视图引擎一样开始使用它,例如:

app.get("/", function(req, res) {
    res.render("index", {"title": "title goes here"});
});

My post is a shameless plug, but it might help someone out.

I wasn't very happy with the way existing modules worked with Express 3.x, I wrote one called dot-emc:

https://github.com/nerdo/dot-emc

Usage is similar to what has been posted above. Install it with nom:

npm install dot-emc

Then set it up as your default view engine. I prefer using the .def extension since my text editor recognizes .dot files as Graphviz files, so the syntax is slightly different:

app.engine("def", require("dot-emc").__express);
app.set("view engine", "def");

Then you can start using it as you would any other view engine in your routes, e.g.:

app.get("/", function(req, res) {
    res.render("index", {"title": "title goes here"});
});
扬花落满肩 2025-01-10 17:25:35

如果您运行的是express 3,则尚不支持。但是,您可以使用express-dot:

npm installexpress-dot

然后在配置中

app.set('view engine', 'dot' );
app.engine('dot', require('express-dot').__express );

然后在路由中:

res.render('profile', {}); // you will need to create views/profile.dot

If you're running express 3, it's not supported yet. You can however use express-dot:

npm install express-dot

Then in configure

app.set('view engine', 'dot' );
app.engine('dot', require('express-dot').__express );

Then in routes:

res.render('profile', {}); // you will need to create views/profile.dot
欲拥i 2025-01-10 17:25:35

我知道这是一个老问题,但我最近想使用标准生成的 Express 4.xx 应用程序来测试 doT。我没有找到 @olado 的明确示例来轻松与我生成的应用程序匹配。我尝试了不同的插件(让它工作,但不满意),所以我最终编写了这样的模板引擎,以便获得支持包含(#)的预编译点文件,而无需任何额外的插件:

var users = require('./routes/users');
// Standard app above this

var dot = require("dot").process({ 
  path: (__dirname + "/views")
});

var app = express();

// view engine setup
app.engine('dot', function(template, options, cb){
    // using .dot files
    var temp = path.parse(template).name;

    var cont = dot[temp](options);

    return cb(null, cont);


    // Or as one liner
    // return cb(null, dot[path.parse(template).name](options));

    // If you want to do error checking, return the error as callback functions first arg
    // return cb(new Error('Something went wrong');
});

app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'dot');

// Standard generated app below this

现在我可以使用在像这样的路由中以标准的“res.render”方式(对于index.js):

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

记住在.dot模板文件中使用{{it.value}}。在上面的基本示例中,index.dot 看起来像这样:

<!DOCTYPE html>
<html>
  <head>
    <title>{{=it.title}}</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>{{=it.title}}</h1>
    <p>Welcome to {{=it.title}}</p>
  </body>
</html>

I know this is an old question, but I recently wanted to test doT with a standard generated Express 4.x.x app. I did not find the express example from @olado to match easy with my generated app. I tried different plugins (getting it to work, but not satisfied), so I ended up writing the template engine like this in order to get pre-compiled dot files with support for includes(#) without any extra plugin:

var users = require('./routes/users');
// Standard app above this

var dot = require("dot").process({ 
  path: (__dirname + "/views")
});

var app = express();

// view engine setup
app.engine('dot', function(template, options, cb){
    // using .dot files
    var temp = path.parse(template).name;

    var cont = dot[temp](options);

    return cb(null, cont);


    // Or as one liner
    // return cb(null, dot[path.parse(template).name](options));

    // If you want to do error checking, return the error as callback functions first arg
    // return cb(new Error('Something went wrong');
});

app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'dot');

// Standard generated app below this

Now I can use it in the standard "res.render" way in the routes like this (for index.js):

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

Remember to use {{it.value}} in the .dot template files. In basic example above, the index.dot would look something like this:

<!DOCTYPE html>
<html>
  <head>
    <title>{{=it.title}}</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>{{=it.title}}</h1>
    <p>Welcome to {{=it.title}}</p>
  </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文