簡體   English   中英

將Node JS變量傳遞給EJS模板

[英]Pass node js variable to ejs template

我在節點js文件“ index.js”中有發布請求

this.app.post('/profile', (req, res) => {                        
            let password = req.body.password;            
            let newWallet = operator.createWalletFromPassword(password);
            let projectedWallet = projectWallet(newWallet);
            res.render('profile.ejs', {
                user : req.user,
            });
            console.log(JSON.stringify(projectedWallet));        
        });

在profile.ejs中顯示給客戶看,我有:這是我按要求進行編輯的完整代碼。

<html lang="en">
<head>
    <title>Node Authentication</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
    <style>
        body        { padding-top:80px; word-wrap:break-word; }
    </style>
</head>
<br>
   <%- include header %>

   <div class="container">

    <div class="page-header text-center">
        <h1><span class="fa fa-anchor"></span> Profile Page</h1>
        <a href="/logout" class="btn btn-default btn-sm">Logout</a>
        <form action="/profile" method="get">
            <button type="submit" class="btn btn-warning btn-lg" id="sub" >test</button>
        </form> 
    </div>

    <div class="row">

        <!-- LOCAL INFORMATION -->
        <div class="col-sm-6">
            <div class="well">
                <h3><span class="fa fa-user"></span> Local</h3>
                    <form action="/profile" method="post">
                        <p>                                                    
                            <strong>id</strong>: <%= user.id %><br>
                            <strong>username</strong>: <%= user.username %><br>
                            <strong>password</strong>: <%= user.password %>

                        </p>                                               
                        <textarea id="myTextArea" cols=50 rows=10><%= data %>
                        </textarea>
                        <!-- these fields will be sent to server -->
                        <input type="hidden" name="username" value="<%= user.username %>">
                        <input type="hidden" name="password" value="<%= user.password %>">
                        <button type="submit" class="btn btn-warning btn-lg" id="sub" >Wallet</button>
                    </form>


            </div>
        </div>

    </div>

</div>
    <%- include footer %>

我想要的是在post請求中將“ projectedWallet ”的值放在ejs文件的textarea中,但是我不知道該怎么做。

您將希望通過EJS將projectedWallet饋送到textarea中,如下所示:

index.js

this.app.post('/profile', (req, res) => {                        
  let password = req.body.password;            
  let newWallet = operator.createWalletFromPassword(password);
  let projectedWallet = projectWallet(newWallet);
  res.render('profile.ejs', {
    user : req.user,

    // We are now feeding your EJS template another variable
    projectedWallet : JSON.stringify(projectedWallet),
  });
  console.log(JSON.stringify(projectedWallet));        
});

...並在模板的文本區域內使用它

profile.ejs

<form action="/profile" method="post">
  <p>                                                    
   <strong>id</strong>: <%= user.id %><br>
   <strong>username</strong>: <%= user.username %><br>
   <strong>password</strong>: <%= user.password %>                         
  </p>                                               
  <textarea id="myTextArea" cols=50 rows=10>

    <!-- We now populate the template with the sent variable -->
    <%= projectedWallet %>

  </textarea>
  <!-- these fields will be sent to server -->
  <input type="hidden" name="username" value="<%= user.username %>">
  <input type="hidden" name="password" value="<%= user.password %>">
  <button type="submit" class="btn btn-warning btn-lg">Wallet</button>
 </form>

您可以將變量保存在響應的局部變量中,然后在前端使用它(在這種情況下為EJ)。

更新的代碼::

index.js

this.app.post('/profile', (req, res) => {                        
            let password = req.body.password;            
            let newWallet = operator.createWalletFromPassword(password);
            let projectedWallet = projectWallet(newWallet);
            //added line
            res.locals.projectedWallet =  projectedWallet
            res.render('profile.ejs', {
                user : req.user,
            });
            console.log(JSON.stringify(projectedWallet));        
        });

profile.ejs

<form action="/profile" method="post">
    <p>                                                    
        <strong>id</strong>: <%= user.id %><br>
        <strong>username</strong>: <%= user.username %><br>
        <strong>password</strong>: <%= user.password %>                         
    </p>   
      <!-- updated line -->                                  
    <textarea id="myTextArea" cols=50 rows=10><%=projectedWallet %>
    </textarea>
    <!-- these fields will be sent to server -->
    <input type="hidden" name="username" value="<%= user.username %>">
    <input type="hidden" name="password" value="<%= user.password %>">
    <button type="submit" class="btn btn-warning btn-lg">Wallet</button>
</form>

您需要檢查是否定義了變量( projectedWallet ),在.get請求中您沒有將projectedWallet作為參數發送,因此這就是為什么出現錯誤的原因

<% if(typeof projectedWallet !== 'undefined') { %>
   <p><%= projectedWallet %></p>
<% } %>

如果多次渲染視圖有時有時使用變量,而其他時候不使用變量,則需要添加條件並檢查每個渲染是否存在該變量

暫無
暫無

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

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