Iteration in Pug

Iteration is the execution of a set of instructions repeatedly until the condition is met. It reduces the task of manually writing the same code again and again. Iteration is used to handle large amounts of data. Due to flexibility, we can iterate over different data structures like arrays, lists, or objects. In pug we can iterate using the each and while method.

Syntax:

ul 
each value in [ element of the array ]
li = value
  • each method is used to iterate over the array and the object.

Syntax:

-  var n=value
ul
- while(condition)
//- update condition
li =n

Pug View Engine – Iteration

Pug is a template engine for NodeJS and browsers to render dynamic reusable content. At compile time, the template engine compiles our Pug template code to HTML. Pug has many powerful features like conditions, loops, includes, and mixins using which we can render HTML code based on user input or reference data.

Pug also supports JavaScript natively, hence using JavaScript expressions, we can format HTML code. This approach allows us to reuse static web pages which have dynamic data. Angular brackets are not used while specifying the tags.

Table of Content

  • Iteration in Pug
  • Approach to Implement Iteration in Pug
  • Steps to Implement Iteration in Pug View Engine

Similar Reads

Iteration in Pug:

Iteration is the execution of a set of instructions repeatedly until the condition is met. It reduces the task of manually writing the same code again and again. Iteration is used to handle large amounts of data. Due to flexibility, we can iterate over different data structures like arrays, lists, or objects. In pug we can iterate using the each and while method....

Approach to Implement Iteration in Pug:

Express Setup: Initializes an Express.js server.Setting View Engine: Configures Pug as the view engine for rendering templates.Routing: Defines a single route for the root URL (/). When accessed, it renders the Pug template.Pug Template: The Pug template defines the structure and content of the HTML page. It includes iteration items with headers Styling: Internal CSS is used within the Pug template (style.) to set margins and styles for headings and other components....

Steps to Implement Iteration in Pug View Engine:

Step 1: Create a NodeJS Application using the following command:...

Folder Structure:

Folder Structure...