Skip to content

Instantly share code, notes, and snippets.

@jwcastillo
Forked from kanwarujjaval/form.ejs
Created October 8, 2015 21:26
Show Gist options
  • Save jwcastillo/9bd5a03af16895d779d5 to your computer and use it in GitHub Desktop.
Save jwcastillo/9bd5a03af16895d779d5 to your computer and use it in GitHub Desktop.
Creating multiple forms with a single ejs file in node.js
<form action="<%= action %>" method="post">
<% if (fields.length) { %>
<% fields.forEach(function(field){ %>
<label><%= field.name %></label>
<input type="<%= field.type %>" name="<%= field.name %>" <% if (field.property) { %> <%= field.property %> <% } %> >
<% }) %>
<% } %>
<button type="submit"><%= title %></button> <!--Title for button is same as that of the page-->
</form>
app.get('/login', function (req, res) {
res.render('form', {
title: "Login", //page title
action: "/login", //post action for the form
fields: [
{name:'email',type:'text',property:'required'}, //first field for the form
{name:'password',type:'password',property:'required'} //another field for the form
]
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment