2013-03-24 73 views
3

我試圖在Express中創建一個原始商店前臺應用程序,並試圖設置它,以便如果某人登錄,所以用戶名不是'未定義',它會顯示一個區塊,並且如果您登錄它顯示另一個。是否有任何方法可以在玉石中使用if/else塊?

這是我的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 
     -} 

然後,我可以看到在日誌中的文本。

回答

3

你的代碼是正確的,但你通過username渲染玉文件時。翡翠由您的快遞服務器渲染以迴應請求,並在渲染髮生時執行其他代碼。

如果您在顯示頁面後登錄(設置用戶名)或註銷,則該塊將不會顯示/更新。在您的應用程序,你可以通過用戶名,而渲染

res.render('layout', {locals:{title: 'Page title', username: "something"}}); 
//display block content 
res.render('layout', {locals:{title: 'Page title'}}); 
//display block items 
3

使用underscore.Js爲了更好地管理和地圖HTML玉,這是最好的功能

在該文件中的配置明示或連接

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

In 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 

它好多了,你可以做interes事情!

+0

請停止破壞你的帖子。請讓mod知道你是否有問題。 – 2016-02-18 17:55:44

相關問題