Skip to content

Instantly share code, notes, and snippets.

@lkacenja
Created October 8, 2013 16:46
Show Gist options
  • Save lkacenja/6887646 to your computer and use it in GitHub Desktop.
Save lkacenja/6887646 to your computer and use it in GitHub Desktop.
Adapted views-view-table template for Twig in Drupal 7. Approach would not be appropriate for D8 (or necessary).
<?php
// Preprocess
function minim_preprocess_views_view_table(&$variables) {
if (!empty($variables['header'])) {
$variables['headers_rendered'] = '';
foreach ($variables['header'] as $field => $label) {
$variables['headers_rendered'] .= "<th class='" . $variables['header_classes'][$field] . "'>" . $label . "</th>";
}
}
if (!empty($variables['rows'])) {
$variables['rows_rendered'] = '';
foreach ($variables['rows'] as $row_count => $row) {
if (!empty($variables['row_classes'][$row_count])) {
$variables['rows_rendered'] .= "<tr class='" . implode(' ', $variables['row_classes'][$row_count]) . "'>";
}
else {
$variables['rows_rendered'] .= "<tr>";
}
foreach ($row as $field => $content) {
if (!empty($variables['field_classes'][$field][$row_count])) {
$variables['rows_rendered'] .= "<td class='" . $variables['field_classes'][$field][$row_count] . "'" . drupal_attributes($variables['field_attributes'][$field][$row_count]) .">" . $content . "</td>";
}
else {
$variables['rows_rendered'] .= "<td " . drupal_attributes($variables['field_attributes'][$field][$row_count]) . ">" . $content . "</td>";
}
}
$variables['rows_rendered'] .= "</tr>";
}
}
}
// Template
<table class="{{classes}}" {{attributes}}>
{% if title or caption %}
<caption>{{caption}}{{title}}</caption>
{% endif %}
{% if headers_rendered %}
<thead>
<tr>
{{ headers_rendered }}
</tr>
</thead>
{% endif %}
{% if rows_rendered %}
<tbody>
{{ rows_rendered }}
</tbody>
{% endif %}
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment