ejs包括不会传递局部变量

时间:2015-11-05 14:50:10

标签: node.js ejs

将nodejs和expressjs与ejs一起用作模板引擎。

我的一条路线使用:

res.render('register',{
  title: "Register",
  classname: "register",
  regdata: req.body,
  socialIcons: socialIcons,
  err: "pass"
});

register.ejs文件如下所示:

<!DOCTYPE html>
<% console.log('In the template it gives:'); console.log(regdata) %>
<html lang="en">
  <head><% include partials/template/head.ejs %></head>

  <body class="<%= classname %>">
    <% include partials/template/header.ejs %>
    <% include partials/content/userregisterbody %>
    <% include partials/template/footer.ejs %>
    <% include partials/template/jsdefaults.ejs %>
  </body>
</html>

注意第二行console.log(regdata)。这打印出控制台应该存在的内容,如下所示:

{username: "jim", 
password:"abc", 
passwordConfirm: "cde",
email: "jim@jim.com",
fullName: "Jim O Tool"}
一切顺利。我的问题是我include partials/content/userregisterbody

这是一个较长的ejs文件。它应该使用两个局部变量regdataerr

在这个文件的第一行,我有:

<% console.log(regdata) %>
<% console.log(err) %>

这个输出是:

undefined
'pass'

为什么一个局部变量被传递给包含的ejs文件但另一个不是?为什么regdata现在出现undefined?有什么建议?提前谢谢。

部分代码:

<% console.log('But in userregisterbody:'); console.log(regdata); console.log(socialIcons); console.log(err) %>
<% if (err) { if (err === "pass") {regdata.password = ""; regdata.passwordConfirm = "";} else if (err === "user") { regdata.username = ""; } }%>
<% if (!regdata) {var regdata = new Object(); regdata.fullname = ""; regdata.password = ""; regdata.passwordconfirm =""; regdata.email=""; regdata.username = ""} %>

<div class=container>
  <div class="row">
    <div class="col-sm-3"></div> <!--col-->
    <div class="col-sm-6">
      <form class="form-signin" action="" method="post">
        <h2 class="form-signin-heading">Register account for GetOutDaHouse</h2>
        <label class="sr-only" for="inputPassword">Full Name</label>
        <input id="inputPassword" class="form-control" autofocus="" type="text" name="fullname" required="" placeholder="Full Name" value="<%=regdata.fullName%>" ></input>
        <label class="sr-only" for="inputPassword">Username</label>
        <input id="inputPassword" class="form-control" type="text" name="username" required="" placeholder="Username"></input>
        <label class="sr-only" for="inputEmail">Email address</label>
        <input id="inputEmail" class="form-control" type="email" name="email"  required="" placeholder="Email address"></input>
        <label class="sr-only" for="inputPassword">Password</label>
        <input id="inputPassword" class="form-control" type="password" name="password" required="" placeholder="Password"></input>
        <label class="sr-only" for="inputPassword">Confirm Password</label>
        <input id="inputPassword" class="form-control" type="password" name="passwordconfirm" required="" placeholder="Confirm Password"></input>
        <div class="checkbox">
          <label></label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>
      <br/>
      <p>Not a Customer? New Activity Camp? Would you like to advertise your services? Register <a href="/campregister">here</a></p>
    </div> <!--col-->
    <div class="col-sm-3"></div> <!--col-->
  </div> <!--row-->
</div> <!--container-->

本地对象socialIcons也可以正常传递。它是一系列对象:

[
  { site: 'facebook',
    thumnail: 'facebookthumnail.jpg',
    url: 'www.facebook.com'},
  { site: 'instagram',
  ...
  }]

哪里......应该是显而易见的。

0 个答案:

没有答案