Use Pug For Loops to Generate Form Markup

Jhey Tompkins
InstructorJhey Tompkins
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

Use the HTML preprocessor Pug to generate markup for a form. Here we generate a set of labels and checkboxes. We link these using ES6 Template Literals with a loop index.

Jhey Tompkins: [0:00] We're going to use a form for our game, so we create a form element with class name .game. A tic-tac-toe board consists of nine cells. Each of those cells provides the opportunity to either place a naught or a cross.

[0:13] What we can do is use a for loop to generate 18 checkboxes. For each iteration, we create two checkboxes, input(type='checkbox'). Then we can use template literals to generate an id. Here, we're going to do x-, and then the index in the loop. We can do the same for naught with o- and the index of the loop. This will generate 18 checkboxes. We can see here the generated ids through to 8.

[0:43] Next, we need an element for the board. We're going to create a div with the class name .board. This is where we're going to generate cells for the board. We can use the same for loop as before, for(let i = ; i < 9; i++).

[0:58] For each iteration we're going to create a div with the class .board__cell. Inside this element we're going to create two labels to control the checkboxes. We can use the for attribute of a template literal to match up to the ids we defined for the checkboxes.

[1:12] Here we have for= which should match up to input(type='checkbox' id=). Let's give our labels some text using the template literal and let's go with Cross, and we'll print out the index. We'll can do the same for Naught where we change the text to Naught and we match up the id with the o. goes to input(type='checkbox') id=).

[1:40] If we look at our page, we now have 18 checkboxes and 18 labels, and clicking some labels will toggle the state for certain checkboxes.

[1:49] In review, we can use Pug to generate markup for us. In this example we're using for loops. We can use template literals to generate markup based off of the index of our for loops.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today