express+jade:提供的局部变量在视图中未定义(node.js +express+jade)

发布于 2024-10-11 05:32:05 字数 684 浏览 9 评论 0原文

我正在使用node.js 和express 以及jade 模板引擎实现一个web应用程序。

模板渲染得很好,并且可以访问帮助程序和动态帮助程序,但不能访问除“body”局部变量之外的局部变量,“body”局部变量由express提供,并且在我的layout.jade中可用和定义。

这是一些代码:

app.set ('view engine', 'jade');

app.get ("/test", function (req, res) {  
    res.render ('test', {
        locals: { name: "jake" }
    });
});

这是 test.jade:

p hello
=name

当我删除第二行(引用名称)时,模板会正确呈现,在网页中显示单词“hello”。当我包含=name时,它会抛出一个ReferenceError:

500 ReferenceError: Jade:2 NaN. 'p hello' NaN. '=name' name is not defined
NaN. 'p hello'
NaN. '=name'

我相信我正在遵循jade并准确地表达关于局部变量的示例。我做错了什么吗,或者这可能是express或jade的一个错误?

I'm implementing a webapp using node.js and express, using the jade template engine.

Templates render fine, and can access helpers and dynamic helpers, but not local variables other than the "body" local variable, which is provided by express and is available and defined in my layout.jade.

This is some of the code:

app.set ('view engine', 'jade');

app.get ("/test", function (req, res) {  
    res.render ('test', {
        locals: { name: "jake" }
    });
});

and this is test.jade:

p hello
=name

when I remove the second line (referencing name), the template renders correctly, showing the word "hello" in the web page. When I include the =name, it throws a ReferenceError:

500 ReferenceError: Jade:2 NaN. 'p hello' NaN. '=name' name is not defined
NaN. 'p hello'
NaN. '=name'

I believe I'm following the jade and express examples exactly with respect to local variables. Am I doing something wrong, or could this be a bug in express or jade?

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

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

发布评论

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

评论(3

攒眉千度 2024-10-18 05:32:06

我认为该错误有时是由于浏览器对 favicon.ico 的请求引起的。

尝试将这些行添加到layout.jade头以链接图标

link(rel='icon', href='/images/siteicon.png')

这消除了我遇到的相同错误

I think the error is sometime caused due to the request by the browser for favicon.ico.

Try adding these lines to the layout.jade head to link the icon

link(rel='icon', href='/images/siteicon.png')

This removed the same error that I was getting

霊感 2024-10-18 05:32:05
app.set ('view engine', 'jade');

app.get ("/test", function (req, res) {  
    res.render ('test', {
        name: "jake"
    });
});

你可以这样做。

app.set ('view engine', 'jade');

app.get ("/test", function (req, res) {  
    res.render ('test', {
        name: "jake"
    });
});

you can do it like this.

花开浅夏 2024-10-18 05:32:05

您可以使用#{变量名},而不是=。这是我如何使用它的示例:
这将呈现一个页面,其中包含用于导航的菜单。每次加载页面时传递页面标题,当然您需要为每个页面创建一个 app.get 函数。

App.js

var navigation = {
  home : {
    uri : "/",
    url : "index",
    title : "Home"
  },
  lab : {
    uri : "/lab",
    url : "lab",
    title : "Lab"
  },
  profile : {
    uri : "/profile",
    url : "profile",
    title : "Profile"
  },
  timetable : {
    uri : "/timetable",
    url : "timetable",
    title : "Timetable"
  }
}

app.get(navigation.profile.uri, function(req, res){ //Profile
  res.render(navigation.profile.url, {
    title: navigation.profile.title,
    navigation: navigation
  });
});

profile.jade

section#page-content
  h1#page-title #{title}
  p Welcome to #{title}

layout.jade

!!! 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/reset.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    header#site-header
      nav#site-navigation
        != partial("partials/navigation")
    section!= body
  footer#page-footer

Rather than =, you can use #{variable-name}. Here is an example of how I'm using it:
This will render a page, with a menu for navigation. Passing the page title in each time the page is loaded, ofcourse you will need to create an app.get function for each page.

App.js

var navigation = {
  home : {
    uri : "/",
    url : "index",
    title : "Home"
  },
  lab : {
    uri : "/lab",
    url : "lab",
    title : "Lab"
  },
  profile : {
    uri : "/profile",
    url : "profile",
    title : "Profile"
  },
  timetable : {
    uri : "/timetable",
    url : "timetable",
    title : "Timetable"
  }
}

app.get(navigation.profile.uri, function(req, res){ //Profile
  res.render(navigation.profile.url, {
    title: navigation.profile.title,
    navigation: navigation
  });
});

profile.jade

section#page-content
  h1#page-title #{title}
  p Welcome to #{title}

layout.jade

!!! 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/reset.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
  body
    header#site-header
      nav#site-navigation
        != partial("partials/navigation")
    section!= body
  footer#page-footer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文