Forms are one of the important components of web application. You need registration form, login form, edit form, status update form etc ..
In this video tutorial we shall see how to write the form in Jade, and how to use Twitter Bootstrap to design the form (we just show you how to add the id or class tags and not actual bootstrap implementation – but if you need any help, you can always ask us in the comment section below).
Related Read:
Basics of Jade – Template Engine: Node.js
Loops and Conditions In Jade: Node.js
Mixins in Jade: Node.js
Route To Render Form
app.js
1 2 3 | app.get('/edit', function(req, res){ res.render('edit-form'); }); |
We define a route /edit and once someone visits this route, we render our edit-form.jade template file.
Form tags in Jade
edit-form.jade
1 2 3 4 5 6 7 8 9 10 | form(method="POST", action="/user") input(type="hidden", name="_method", value="PUT") p Name input(type="text", name="name") p Email input(type="email", name="email") p Age input(type="number", name="age") p input(type="submit") |
Here we indent input tags inside paragraph tags, which is a child of form. We separate form attributes using comma.
Forms Using Jade: Node.js
[youtube https://www.youtube.com/watch?v=1RC9re_llx0]
Form tags with Jade and Bootstrap
edit-form.jade
1 | input#btn(type="submit") |
If you want to use bootstrap along with your jade template files, make sure to include bootstrap.min.css and bootstrap.min.js and jquery.min.js into your file, by opening layout.jade present inside your view folder. Now add the id or class names along with the html tags, this should render you bootstrap enabled functionality and styling.
Form output source code
view-source:localhost:3000/edit
1 2 3 4 5 6 7 | <form method="POST" action="/user"> <input type="hidden" name="_method" value="PUT"/> <p>Name<input type="text" name="name"/></p> <p>Email<input type="email" name="email"/></p> <p>Age<input type="number" name="age"/></p> <p><input type="submit"/></p> </form> |
You can observe the nesting of elements and their relationship – child and parent.