table.pug – Tables generation

View source code on Github

Table from JSON matrix

mixin table-json(src, header=true)
  //- requires context --> {require: require}
  - matrix_table = require(src);
  - count = 0;
  table&attributes(attributes)
    each row in matrix_table
      tr
        each column in row
          - if (count == 0)
            - if (header == true)
              th #{column}
            - else
              td #{column}
          - else
            td #{column}
        - count += 1;
+table-json(src, header=true)

Note

This mixin requires require_context inyection.

Create a table indicating a JSON file path which has a two level matrix structure (arrays inside arrays) like:

[
    [1, 2, 3, 4, 5],
    [1, 2, 3, 4, 5]
]
Arguments:
  • src (string) – Path of the .json file to load.
  • header (bool, optional) – Indicates if include th tags for the first row. As default true.

Usage

Inputs

[
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]
+table-json("data.json")(style="width:50%;")

Output

<table style="width:50%;">
  <tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
    <th>4</th>
    <th>5</th>
  </tr>
  <tr>
    <td>6</td>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    <td>10</td>
  </tr>
  <tr>
    <td>11</td>
    <td>12</td>
    <td>13</td>
    <td>14</td>
    <td>15</td>
  </tr>
</table>

Render

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15