Skip to content

Instantly share code, notes, and snippets.

@kanwarujjaval
Created February 23, 2014 13:54
Show Gist options
  • Save kanwarujjaval/9171795 to your computer and use it in GitHub Desktop.
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
]
});
});
@sylvery
Copy link

sylvery commented Mar 28, 2017

This is much easier and simpler to implement with EJS than with Angular! Thanks a bunch!

@blendedmarket
Copy link

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