问题
Using nodejs and expressjs with ejs as template engine.
One of my routes uses:
res.render('register',{
title: "Register",
classname: "register",
regdata: req.body,
socialIcons: socialIcons,
err: "pass"
});
The register.ejs file looks like this:
<!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>
Notice on the second line the console.log(regdata)
. This prints exactly what should be there to the console, which looks like:
{username: "jim",
password:"abc",
passwordConfirm: "cde",
email: "jim@jim.com",
fullName: "Jim O Tool"}
All going good. My problem is when I include partials/content/userregisterbody
.
This is a longer ejs file. It should use two of the local variables, regdata
and err
.
In the first line of this file I have:
<% console.log(regdata) %>
<% console.log(err) %>
The output of this is:
undefined
'pass'
Why is one local variable getting passed on to the included ejs file but the other is not?? Why does regdata now come up as undefined
? Any suggestions? Thanks in advance.
Partials code :
<% 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-->
The local object socialIcons
gets passed fine too. It's an array of objects:
[
{ site: 'facebook',
thumnail: 'facebookthumnail.jpg',
url: 'www.facebook.com'},
{ site: 'instagram',
...
}]
where ... should be obvious.
来源:https://stackoverflow.com/questions/33547629/ejs-include-wont-pass-local-variable