簡體   English   中英

如何使用 node-express 在包含的 pug 模板中傳遞數據

[英]How to pass data in included pug template using node-express

-- header.jade
ul
    li
        a User = #{userName}

-- layout.jade
doctype html
html
  head
    title= title
  body
    include header

    include index

-- route file: index.js
router.get('/', function(req, res, next) {
  res.render('index', {
            userName: 'Jane Doe'
        });
});

如何獲取 layout.jade 文件中包含的 header jade 模板中的 userName 值。 我正在嘗試為我的項目實現一個通用的標題概念。

任何幫助將不勝感激! TIA

從最后一行中取出return 現在它也叫pug而不是jade

您以錯誤的方式返回您的回復。

使用這樣的所需模板返回您的響應數據

 router.get('/', function(req, res, next) {
 return res.render('index', {
    userName: 'Jane Doe'
  });
});

你想要的行為現在是默認值,變量被傳遞到包含的視圖。 如果需要,還可以使用混合函數,請參閱Jade include with parameter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM