1

Is there a way to return the number of columns in a table?

I tried {{ entry.tableHandle | length }} but this seems to return the number of rows rather than columns.

I've got a site with a couple of tables and I'm trying to output the number of columns in a given table to use for my CSS responsive column width.

I.E. If table was 2 columns, it would output a 2 and I would use that to set column width to 50% etc...

1

entry.tableHandle is an array of all of the rows, and each row is an array with keys that are the column names and values that are the column values AND keys that are col1, col2, etc. and values that are the column values.

So you can do entry.tableHandle[0]|keys to get the keys, entry.tableHandle[0]|keys|length to get the number of keys, and finally (entry.tableHandle[0]|keys|length)/2 to get the number of columns.

You will want to check first that there is a row in the table - this won't work if there is no row 0.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.