{{- /* Shortcode csv-to-table: Renders a simple table from a CSV file Named arguments : - src : the source location of the CSV file : "bundle", "asset", "url" ; by default "bundle" - path : the path or URL to the CSV file - id, class : the HTML class & id attributes for the table; by default : auto generated id and class "csvtable" */ -}} {{- /* Check mandatory argument path */ -}} {{- with $path := .Get "path" -}} {{- /* Retrieve data source */ -}} {{- $type := or ($.Get "src" | strings.ToLower) "bundle" -}} {{- $data := 0 -}} {{- if eq $type "bundle" -}} {{- $data = $.Page.Resources.Get $path -}} {{- else if eq $type "asset" -}} {{- $data = resources.Get $path -}} {{- else if eq $type "url" -}} {{- with resources.GetRemote $path -}} {{- with .Err -}} {{- errorf "The %q shortcode encountered an unexpected error %s while trying to load %. See %s" $.Name . $path $.Position -}} {{- else -}} {{- $data = . -}} {{- end -}} {{- else -}} {{- errorf "The %q shortcode could not retrieve remote ressource %s. See %s" $.Name $path $.Position -}} {{- end -}} {{- else -}} {{- errorf "The %q shortcode requires the origin parameter to be one of these strings: bundle,asset,url. See %s" $.Name $.Position -}} {{- end -}} {{- /* Now retrieve other arguments */ -}} {{- $id := or ($.Get "id") "" }} {{- $class := or ($.Get "class") "csvtable" }} {{- /* Define attributes map. */}} {{- $attrs := dict "class" $class "id" $id }} {{/* Parse the data source */}} {{- with $data -}} {{- with . | transform.Unmarshal -}}
{{- range index . 0 -}} {{ end }} {{- range after 1 . -}} {{- range . -}} {{- end -}} {{- end -}}
{{ . }}
{{- . -}}
{{- end -}} {{- else -}} {{/* In case of incorrect data source */}} {{ errorf "The %q shortcode was unable to find %s. See %s" $.Name $path $.Position }} {{ end }} {{ else }} {{/* In case of missing path argument */}} {{ errorf "The %q shortcode requires an argument named path. See %s" .Name .Position }} {{ end }}