Skip to content

Instantly share code, notes, and snippets.

@kanwarujjaval
Created February 23, 2014 13:54
Show Gist options
  • Select an option

  • Save kanwarujjaval/9171795 to your computer and use it in GitHub Desktop.

Select an option

Save kanwarujjaval/9171795 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
]
});
});
@blendedmarket
Copy link
Copy Markdown

So obvious... it's awesome. EJS for the win!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment