3

我正在尝试在 Express 中制作一个粗略的店面应用程序,并尝试对其进行设置,以便如果有人登录,因此用户名不是“未定义”,它会显示一个块,如果你已注销它显示另一个。

这是我的layout.jade文件:

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
      h1.main= title
        body
          - if (typeof(username) !== 'undefined') {
            block items
          -}
          - else {
            block content
          -}

我的内容块内容。

extends layout

block content

  #content
    img#splash(src='images/ninja.png')

我的物品被挡住了。

block items

     #items
       h1 Hello (username)

当我登录时,我只看到页面标题(我的 layout.jade 文件内容),而不是 items.jade 文件的内容。

有人知道为什么是这样吗?我是 Express 的新手,无法解决:/

如果我做类似的事情。

- if (typeof(username) !== 'undefined') {
        p test
      -}
      - else {
        block content
      -}

然后我可以在登录时看到文本。

4

2 回答 2

3

您的代码是正确的,但是您username在渲染玉文件时是否通过了。jam 由您的 express 服务器渲染以返回请求,如果在渲染发生时执行其他代码。

如果您在显示页面后登录(设置用户名)或注销,则该块将不会显示/更新。在您的应用程序中,您可以在渲染时传递用户名

res.render('layout', {locals:{title: 'Page title', username: "something"}});
//display block content
res.render('layout', {locals:{title: 'Page title'}});
//display block items
于 2013-03-24T05:30:53.333 回答
3

使用underscore.Js更好的管理和映射 html 到 Jade,最好是函数

在文件配置中表达或连接

    app.use(function(req, res, next){
       res.locals._ = require('underscore');
       next();
    });

在 index.jade

    doctype 5
    html
      head
        title= title
        link(rel='stylesheet', href='/stylesheets/style.css')
          h1.main= title
            body
              if _.isUndefined( username )
              //- if _.isString( username ) or bla...
                block items
              else
                block content

它好多了,你可以做有趣的事情!

于 2013-03-25T03:27:34.523 回答