Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tables: Consider enabling array iteration in macros #570

Open
paroxp opened this issue Mar 2, 2018 · 7 comments
Open

Tables: Consider enabling array iteration in macros #570

paroxp opened this issue Mar 2, 2018 · 7 comments
Labels
feature request User requests a new feature nunjucks table

Comments

@paroxp
Copy link
Member

paroxp commented Mar 2, 2018

What

We've got an use case, where we'd like to pass some data into the template without pre-processing it to fit the table macro.

Our use case using pure HTML:

<table>
{% for product in products %}
  <tr>
    <td>{{ product.name }}</td>
    <td>{{ product.description }}</td>
    <td>{{ product.price }}</td>
  </tr>
{% endfor %}
</table>

What we'd like to be able to do when using macros:

{{ govukTable({
  "firstCellIsHeader": true,
  "head":[
    {"text":"Name"},
    {"text":"Description"},
    {"text":"Price"}
  ],
  "data": products,
  "rows": [
    {"text": product.name},
    {"text": product.description},
    {"text": product.price},
  ]
}) }}
@NickColley NickColley added the feature request User requests a new feature label Mar 2, 2018
@NickColley
Copy link
Contributor

See also: Digital Marketplace's approach to tables: http://alphagov.github.io/digitalmarketplace-frontend-toolkit/summary-table.html

@NickColley NickColley changed the title Consider enabling array iteration in macros Tables: Consider enabling array iteration in macros Mar 16, 2018
@NickColley
Copy link
Contributor

See Tijmen's look into this from a Ruby perspective: alphagov/govuk_publishing_components#337

@NickColley
Copy link
Contributor

Another look into how our tables are being integrated into GOV.UK Publishing Components: https://github.com/alphagov/govuk_publishing_components/pull/531/files#r219539033

@trang-erskine
Copy link

Already done.

@joelanman
Copy link
Contributor

Checked with @36degrees - not sure this was done?

@joelanman
Copy link
Contributor

joelanman commented May 2, 2024

just thinking about the proposal, product is undefined. I'm thinking something like this instead:

products = [
  { name: "Desk", descriptionHTML: "<p>a nice desk</p>", price: "£100" },
  { name: "Phone", descriptionHTML: "<p>a nice phone</p>", price: "£200" },
  { name: "Pen", descriptionHTML: "<p>a nice pen</p>", price: "£1" }
]

{{ govukTable({
  "firstCellIsHeader": true,
  "head":[
    {"text":"Name"},
    {"text":"Description"},
    {"text":"Price"}
  ],
  "data": products,
  "columns": [
    {"text": "name"},
    {"html": "descriptionHTML"},
    {"text": "price"}
  ]
}) }}

or for an array of arrays, not objects:


products = [
  [ "Desk", "<p>a nice desk</p>", "£100" ],
  [ "Phone", "<p>a nice phone</p>", "£200" ],
  [ "Pen", "<p>a nice pen</p>", "£1" ]
]

{{ govukTable({
  "firstCellIsHeader": true,
  "head":[
    {"text":"Name"},
    {"text":"Description"},
    {"text":"Price"}
  ],
  "data": products,
  "columns": [
    {"text": 0},
    {"html": 1},
    {"text": 2}
  ]
}) }}

@joelanman
Copy link
Contributor

columns could also just be a second argument to data:

"data": products, [
    {"text": "name"},
    {"html": "descriptionHTML"},
    {"text": "price"}
  ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request User requests a new feature nunjucks table
Projects
None yet
Development

No branches or pull requests

8 participants