table.html.twig 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. {#
  2. /**
  3. * @file
  4. * Theme override to display a table.
  5. *
  6. * Available variables:
  7. * - attributes: HTML attributes to apply to the <table> tag.
  8. * - caption: A localized string for the <caption> tag.
  9. * - colgroups: Column groups. Each group contains the following properties:
  10. * - attributes: HTML attributes to apply to the <col> tag.
  11. * Note: Drupal currently supports only one table header row, see
  12. * https://www.drupal.org/node/893530 and
  13. * http://api.drupal.org/api/drupal/includes!theme.inc/function/theme_table/7#comment-5109.
  14. * - header: Table header cells. Each cell contains the following properties:
  15. * - tag: The HTML tag name to use; either 'th' or 'td'.
  16. * - attributes: HTML attributes to apply to the tag.
  17. * - content: A localized string for the title of the column.
  18. * - field: Field name (required for column sorting).
  19. * - sort: Default sort order for this column ("asc" or "desc").
  20. * - sticky: A flag indicating whether to use a "sticky" table header.
  21. * - rows: Table rows. Each row contains the following properties:
  22. * - attributes: HTML attributes to apply to the <tr> tag.
  23. * - data: Table cells.
  24. * - no_striping: A flag indicating that the row should receive no
  25. * 'even / odd' styling. Defaults to FALSE.
  26. * - cells: Table cells of the row. Each cell contains the following keys:
  27. * - tag: The HTML tag name to use; either 'th' or 'td'.
  28. * - attributes: Any HTML attributes, such as "colspan", to apply to the
  29. * table cell.
  30. * - content: The string to display in the table cell.
  31. * - active_table_sort: A boolean indicating whether the cell is the active
  32. table sort.
  33. * - footer: Table footer rows, in the same format as the rows variable.
  34. * - empty: The message to display in an extra row if table does not have
  35. * any rows.
  36. * - no_striping: A boolean indicating that the row should receive no striping.
  37. * - header_columns: The number of columns in the header.
  38. *
  39. * @see template_preprocess_table()
  40. */
  41. #}
  42. <table{{ attributes }}>
  43. {% if caption %}
  44. <caption>{{ caption }}</caption>
  45. {% endif %}
  46. {% for colgroup in colgroups %}
  47. {% if colgroup.cols %}
  48. <colgroup{{ colgroup.attributes }}>
  49. {% for col in colgroup.cols %}
  50. <col{{ col.attributes }} />
  51. {% endfor %}
  52. </colgroup>
  53. {% else %}
  54. <colgroup{{ colgroup.attributes }} />
  55. {% endif %}
  56. {% endfor %}
  57. {% if header %}
  58. <thead>
  59. <tr>
  60. {% for cell in header %}
  61. {%
  62. set cell_classes = [
  63. cell.active_table_sort ? 'is-active',
  64. ]
  65. %}
  66. <{{ cell.tag }}{{ cell.attributes.addClass(cell_classes) }}>
  67. {{- cell.content -}}
  68. </{{ cell.tag }}>
  69. {% endfor %}
  70. </tr>
  71. </thead>
  72. {% endif %}
  73. {% if rows %}
  74. <tbody>
  75. {% for row in rows %}
  76. {%
  77. set row_classes = [
  78. not no_striping ? cycle(['odd', 'even'], loop.index0),
  79. ]
  80. %}
  81. <tr{{ row.attributes.addClass(row_classes) }}>
  82. {% for cell in row.cells %}
  83. <{{ cell.tag }}{{ cell.attributes }}>
  84. {{- cell.content -}}
  85. </{{ cell.tag }}>
  86. {% endfor %}
  87. </tr>
  88. {% endfor %}
  89. </tbody>
  90. {% elseif empty %}
  91. <tbody>
  92. <tr class="odd">
  93. <td colspan="{{ header_columns }}" class="empty message">{{ empty }}</td>
  94. </tr>
  95. </tbody>
  96. {% endif %}
  97. {% if footer %}
  98. <tfoot>
  99. {% for row in footer %}
  100. <tr{{ row.attributes }}>
  101. {% for cell in row.cells %}
  102. <{{ cell.tag }}{{ cell.attributes }}>
  103. {{- cell.content -}}
  104. </{{ cell.tag }}>
  105. {% endfor %}
  106. </tr>
  107. {% endfor %}
  108. </tfoot>
  109. {% endif %}
  110. </table>