javascript 在 node.js 中使用带有 express 的部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10222411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:06:03  来源:igfitidea点击:

using partials with express in node.js

javascriptnode.jsexpresspugpartials

提问by Nic Meiring

I am trying to render partials using node.js. Here is my code.

我正在尝试使用 node.js 呈现部分。这是我的代码。

app.js:

应用程序.js:

  var express = require('express')
  , routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

var products = require('./products.js');

app.get('/products', function(req, res) {
    res.render('products/index', {locals: {
               products: products.all
               }
               });
});

app.listen(3000);

When I go to localhost:3000/products it should render index.jade which is in the products folder which is in the views folder.Above I set the views directory using app.set('views', __dirname + '/views');

当我转到 localhost:3000/products 时,它应该呈现位于视图文件夹中的产品文件夹中的 index.jade。上面我使用设置视图目录 app.set('views', __dirname + '/views');

index.jade:

index.jade:

h1 Products:
#products!= partial('partials/product', {collection: products})

This should render the partial equivalent to (partials/product.jade) because jade is my view engine.

这应该呈现相当于 (partials/product.jade) 的部分,因为 jade 是我的视图引擎。

I am getting an error back saying "partial is not defined"

我收到一条错误消息,说“部分未定义”

Any help would be great. thanks

任何帮助都会很棒。谢谢

UPDATE:

更新:

That solved my partial error thank you. I reinstalled 2.5.9.

这解决了我的部分错误,谢谢。我重新安装了 2.5.9。

回答by Jonathan Lonowski

Check what version of Express JS you have installed -- you may have the 3.0 alpha:

检查您安装的 Express JS 版本——您可能有 3.0 alpha:

$ npm ls
...
└─┬ [email protected]
...

If you're interested in trying the alpha, be sure to checkout the documentation on Migrating from 2.x to 3.x. In it, you'll notice that res.partial()and partial()(within templates) have been removed -- as described under "View system changes:"

如果您有兴趣尝试 alpha,请务必查看有关从 2.x 迁移到 3.x的文档。在其中,您会注意到res.partial()partial()(在模板中)已被删除——如“查看系统更改”中所述:

By removing the concept of a "layout" & partials in Express 3.x template engines will have greater control over file I/O. This means integration with template engines much easier, and greatly simplify the view system's internals.

通过在 Express 3.x 模板引擎中删除“布局”和部分的概念,将更好地控制文件 I/O。这意味着与模板引擎的集成更容易,并大大简化了视图系统的内部结构。

You can see an example of the intent in the linked article, Use Jade blocks, not layouts.

您可以在链接的文章Use Jade blocks, not layouts 中看到意图的示例。

If you're not interested, then just make sure you have 2.x installed.

如果您不感兴趣,那么请确保您安装了 2.x。

$ npm install [email protected]

Or via package.json:

或通过package.json

{
    ...
    "dependencies": {
        ...
        "express": "2.x"
    }
}