最佳答案
我正在使用 Node.js 的 Forms 库(表格) ,它将在后端为我呈现一个表单,如下所示:
var signup_form = forms.create({
username: fields.string({required: true})
, password: fields.password({required: true})
, confirm: fields.password({
required: true
, validators: [validators.matchField('password')]
})
, email: fields.email()
});
var signup_form_as_html = signup_form.toHTML();
最后一行 var signup_var signup_form_as_html = signup_form.toHTML();
创建了一个 HTML 块,它看起来是这样的:
<div class="field required"><label for="id_username">Username</label><input type="text" name="username" id="id_username" /></div><div class="field required"><label for="id_password">Password</label><input type="password" name="password" id="id_password" /></div><div class="field required"><label for="id_confirm">Confirm</label><input type="password" name="confirm" id="id_confirm" /></div><div class="field"><label for="id_email">Email</label><input type="text" name="email" id="id_email" /></div>
Basically just a long string of HTML. I then try to render it using EJS and Express using the following code:
res.render('signup.ejs', {
session: loginStatus(req)
, form: signup_form_as_html
});
但是在呈现 HTML 时,它只是我上面提到的字符串,而不是实际的 HTML (因此是我想要的表单)。有没有什么方法可以使字符串呈现为实际的 HTML 使用 EJS?还是我得用像杰德这样的东西?