Skip to content
Advertisement

Randomly Populate table rows in Pug files

Until this morning, I did not know what Pug is. However, now, it’s being used in a theme that I am using for work. Ran into this situation.

The current pug file looks like this.

JavaScript

is there any way to fill up this data with javascript, using some random array. For instance, suppose I have an array like this. Later, I could always write some simple JavaScript code to build a collection with hundreds of names, position and so on.

But, for now, let’s take this array as example.

JavaScript

How could I feed this array to the pug file above?

The original pug file, and the rest of the code/theme/template I am using is available here – https://github.com/StartBootstrap/startbootstrap-sb-admin/blob/master/src/pug/pages/includes/datatable.pug

I have looked at this question, How do I dynamically populate a radio button using Jade/Pug, which seems similar, but I am not able to understand how to include my array into my pug file.

I simply tried putting the object in the file, like this.

JavaScript

That gives an error.

JavaScript

Advertisement

Answer

Note that the – is on a separate line. This will allow you to initialize the variable in pug.

JavaScript

This will resolve your error.

Reference: Write pug array in multiple lines

Further, you can iterate over that array as mentioned below:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement