You could use a shortcode, and move the template logic within the shortcode.
For example, say the shortcode is named layouts/shortcodes/table.html
.
Define it like:
{{ $arg := .Get 0 }}
{{ $dataFile := index .Site.Data $arg }}
{{ $.Scratch.Set "count" 0 }}
<table>
{{ range $table := $dataFile }}
{{ range $row := $table }}
<tr>
{{ range $datas := $row }}
{{ range $data := $datas }}
{{ if eq 0 ($.Scratch.Get "count") }}
<th>
{{ . }}
</th>
{{ else }}
<td>
{{ . }}
</td>
{{ end }}
{{ end }}
{{ end }}
</tr>
{{ $.Scratch.Add "count" 1 }}
{{ end }}
{{ end }}
</table>
Then use it like:
{{< table "sample" >}}
Where "sample"
is the name of your table data file, e.g. data/sample.toml
.