Is it possible to add attribute to <thead> and <tbody>?

hi, I meet some problem when I try to add attribute to the head and body of a table. those was proved incorrect…
1622306358(1)
1622306368(1)
1622306379(1)

No, it is not.

ok, Thanks

You could do it with a custom shortcode.

Given file layouts/shortcodes/custom-table.html with content of:

{{ $table := .Inner | markdownify }}

{{ $tableClass := .Get "tableClass" }}
{{ $theadClass := .Get "theadClass" }}
{{ $tbodyClass := .Get "tbodyClass" }}

{{ $oldTableTag := "<table>" }}
{{ $newTableTag := printf "<table class=\"%s\">" $tableClass }}
{{ $table := replace $table $oldTableTag $newTableTag }}

{{ $oldTheadTag := "<thead>" }}
{{ $newTheadTag := printf "<thead class=\"%s\">" $theadClass }}
{{ $table := replace $table $oldTheadTag $newTheadTag }}

{{ $oldTbodyTag := "<tbody>" }}
{{ $newTbodyTag := printf "<tbody class=\"%s\">" $tbodyClass }}
{{ $table := replace $table $oldTbodyTag $newTbodyTag }}

{{ $table | safeHTML }}

With markdown usage of:

{{< custom-table
  tableClass="sample-table-class"
  theadClass="sample-thead-class"
  tbodyClass="sample-tbody-class"
>}}
| 1 | 2 | 3 |
| --- | --- | --- |
| a | b | c |
{{< /custom-table >}}

Will render this html table:

<table class="sample-table-class">
<thead class="sample-thead-class">
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody class="sample-tbody-class">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
</tbody>
</table>

Git branch: GitHub - zwbetz-gh/hugo-playground at hugo-forum-topic-33145

ok,thanks!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.