aboutsummaryrefslogtreecommitdiff
path: root/content/articles/table-shortcode-examples.md
blob: 3e301457ead6d3dbd0ad4d9716dc67317d2c988f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
---
slug: "table-shortcode-examples"
title: "Table Shortcode Examples"

# date (optional for articles)
date: "2021-10-22T23:11:04-04:00"

# show on articles page
# show: true

# uncomment to show table of contents
toc: true

# table data
tables:
  # available table properties
  table_props:
    name: "Table Properties"

    cols: 
      - id: "id"
        name: "Property"
        tip: "Property ID."
      - id: "required"
        name: "Required?"
        tip: "Is this property required?"
      - id: "text"
        name: "Description"
        tip: "Property description."
      - id: "notes"
        name: "Notes"
        tip: "Property notes."

    rows:
      - id: "`id`"
        required: "No"
        text: "Table ID."
        notes: "Used as the `id` attribute of the `<table>` element."
      - id: "`css`"
        required: "No"
        text: "Table [CSS](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\") class."
        notes: "Appended to the `class` attribute of the `<table>` element."
      - id: "`name`"
        required: "No"
        text: "Table name."
        notes: "Used as the value of the `title` attribute and `aria-label` attribute of the `<table>` element if the `tip` property is not specified."
      - id: "`tip`"
        required: "No"
        text: "Table tooltip."
        notes: "Used as the value of the `title` attribute and `aria-label` attribute of the `<table>`."
      - id: "`config`"
        required: "No"
        text: "Table config."
        notes: "Used to override the configuration for this table."
      - id: "`caption`"
        required: "No"
        text: "Table caption."
        notes: "Value of `<caption>` element for this table."
      - id: "`cols`"
        required: "**Yes**"
        text: "Table columns."
        notes: "Array of column definitions."
      - id: "`rows`"
        required: "**Yes**"
        text: "Table rows."
        notes: "Array of rows."

  # available column properties
  col_props:
    name: "Column Properties"

    cols: 
      - id: "id"
        name: "Property"
        tip: "Property ID."
      - id: "required"
        name: "Required?"
        tip: "Is this property required?"
      - id: "text"
        name: "Description"
        tip: "Property description."
      - id: "notes"
        name: "Notes"
        tip: "Property notes."

    rows:
      - id: "`id`"
        required: "**Yes**"
        text: "Row property which contains the cell value."
        notes: "n/a"
      - id: "`name`"
        required: "**Yes**"
        text: "Column name."
        notes: "Used as the content of `<th>` elements and as the tooltip for `<th>` and `<td>` elements if the `tip` column property is not defined."
      - id: "`tip`"
        required: "No"
        text: "Column tooltip."
        notes: "Used for the `title` and `aria-lable` attributes of `<th>` and `<td>` elements."
      - id: "`align`"
        required: "No"
        text: "Column alignment."
        notes: "One of `left`, `center`, `justify`, or `right`."

  # available row properties
  row_props:
    name: "Row Properties"

    cols: 
      - id: "id"
        name: "Property"
        tip: "Property ID."
      - id: "required"
        name: "Required?"
        tip: "Is this property required?"
      - id: "text"
        name: "Description"
        tip: "Property description."
      - id: "notes"
        name: "Notes"
        tip: "Property notes."

    rows:
      - id: "`_id`"
        required: "No"
        text: "Row ID."
        notes: "Used as the `id` attribute of the generated `<tr>` element."
      - id: "`_css`"
        required: "No"
        text: "Row [CSS class](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\")."
        notes: "Used as value of the `class` attribute of the generated `<tr>` element."
      - id: "`_tip`"
        required: "No"
        text: "Row tooltip."
        notes: "Used as value of the `title` attribute and the `aria-label` attribute of the generated `<tr>` element."

  row_attrs:
    name: "Row Attributes"

    cols: 
      - id: "id"
        name: "Attribute"
        tip: "Attribute name."
      - id: "text"
        name: "Description"
        tip: "Attribute description."
      - id: "notes"
        name: "Notes"
        tip: "Attribute notes."

    rows:
      - id: "`id`"
        text: "n/a"
        notes: "Generated if the `_id` row property is specified."
      - id: "`title`"
        text: "Row tooltip."
        notes: "Generated if the `_tip` row property is specified."
      - id: "`aria-label`"
        text: "Row tooltip."
        notes: "Generated if the `_tip` row property is specified."
      - id: "`class`"
        text: "Row tooltip."
        notes: "Generated if the `_css` row property is specified."
      - id: "`data-tr_y`"
        text: "Zero-indexed Y offset of row position in table."
        notes: "Can be used to match specific rows using [CSS](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\") selectors.  Example `#some-table tr[data-tr_y='2']`."

  # available cell properties
  cell_props:
    name: "Cell Properties"

    cols: 
      - id: "id"
        name: "Property"
        tip: "Property ID."
      - id: "required"
        name: "Required?"
        tip: "Is this property required?"
      - id: "text"
        name: "Description"
        tip: "Property description."
      - id: "notes"
        name: "Notes"
        tip: "Property notes."

    rows:
      - id: "`id`"
        required: "No"
        text: "Cell ID."
        notes: "Used as the `id` attribute of the generated `<td>` element."
      - id: "`tip`"
        required: "No"
        text: "Cell tooltip."
        notes: "Used as value of the `title` attribute and the `aria-label` attribute of the generated `<td>` element."
      - id: "`align`"
        required: "No"
        text: "Cell alignment."
        notes: "One of `left`, `center`, `justify`, or `right`."
      - id: "`html`"
        required: "No"
        text: "Render cell value as HTML?"
        notes: "One of `true` or `false`.  Defaults to `false`."
      - id: "`css`"
        required: "No"
        text: "Cell [CSS class](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\")."
        notes: "Overrides the `align` property if specified."
      - id: "`val`"
        required: "No"
        text: "Cell value."
        notes: "Rendered as [HTML](https://en.wikipedia.org/wiki/HTML \"HyperText Markup Language\") if the `html` property is true, and [Markdown](https://en.wikipedia.org/wiki/Markdown \"Markdown markup language\") otherwise."

  cell_attrs:
    name: "Cell Attributes"

    cols: 
      - id: "id"
        name: "Attribute"
        tip: "Attribute name."
      - id: "text"
        name: "Description"
        tip: "Attribute description."
      - id: "notes"
        name: "Notes"
        tip: "Attribute notes."

    rows:
      - id: "`id`"
        text: "n/a"
        notes: "Generated if the `id` cell property is specified."
      - id: "`title`"
        text: "Row tooltip."
        notes: "Set to the value of `tip` cell property is specified, or followed by the value of the `tip` column property.  Defaults to the name of the column otherwise."
      - id: "`aria-label`"
        text: "Row tooltip."
        notes: "Set to the value of `tip` cell property is specified, or followed by the value of the `tip` column property.  Defaults to the name of the column otherwise."
      - id: "`class`"
        text: "Cell [CSS class](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\")."
        notes: "Generated if the `css` cell property is specified, if
        the cell `align` property is specified, or if the column `align` property is specified."
      - id: "`data-td_x`"
        text: "Zero-indexed X offset of cell position in table."
        notes: "Can be used to match specific columns using [CSS](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\") selectors.  Example `#some-table td[data-td_x='3']`."
      - id: "`data-td_id`"
        text: "Cell column ID."
        notes: "Can be used to match specific columns using [CSS](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\") selectors.  Example `#some-table td[data-td_id='text']`."

  # available column alignments
  align_classes:
    cols:
      - id: "align"
        name: "align"
        tip: "Value of column `align` property."
      - id: "class"
        name: "class"
        tip: "Generated class attribute for <th> and <td> elements."

    rows:
      - align: "`left`"
        class: "`has-text-left`"
      - align: "`center`"
        class: "`has-text-centered`"
      - align: "`justify`"
        class: "`has-text-justified`"
      - align: "`right`"
        class: "`has-text-right`"

  # available table configuration properties
  table_config_props:
    cols: 
      - id: "id"
        name: "Property ID"
        tip: "Property ID."
      - id: "text"
        name: "Description"
        tip: "Property description."
      - id: "notes"
        name: "Notes"
        tip: "Property notes."
      - id: "default"
        name: "Default Value"
        tip: "Default value."

    rows:
      - id: "`data_prefix`"
        text: "Prefix used for all `data-` attributes."
        notes: "Used to prevent data attribute clashes.  For example, if `data_prefix` is set to `zz_`, then the `data-td_x` attribute will be generated as `data-zz_td_z`."
        default: "(empty)"
      - id: "`table_class`"
        text: "Base table [CSS](https://en.wikipedia.org/wiki/CSS \"Cascading Style Sheets\") class."
        notes: "The `css` property of tables is appended to this class to form the class list.  For example, if a table has a `css` property of of `foobar`, then the `<table>` element will be generated with a `class` attribute of `class='table foobar'`."
        default: "`table`"
      - id: "`align_left`"
        text: "Class for left-aligned `<th>` and `<td>` elements."
        notes: "n/a"
        default: "`has-text-left`"
      - id: "`align_center`"
        text: "Class for center-aligned `<th>` and `<td>` elements."
        notes: "n/a"
        default: "`has-text-centered`"
      - id: "`align_justify`"
        text: "Class for justified `<th>` and `<td>` elements."
        notes: "n/a"
        default: "`has-text-justified`"
      - id: "`align_right`"
        text: "Class for right-aligned `<th>` and `<td>` elements."
        notes: "n/a"
        default: "`has-text-right`"

  # example of simple array table
  fruits:
    - ["name", "text"]
    - ["apple", "red"]
    - ["banana", "yellow"]
    - ["grape", "green"]

  # example of columns defined as arrays
  cols_as_array:
    cols: ["name", "text"]
    rows: 
      - ["apple", "red"]
      - ["banana", "yellow"]
      - ["grape", "green"]

  # example of columns and rows defined as hashes
  hashes:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"

  # example of table with caption
  table_caption:
    caption: "an example caption"
    cols: ["name", "text"]
    rows: 
      - ["apple", "red"]
      - ["banana", "yellow"]
      - ["grape", "green"]

  # example of column tooltips
  column_tooltips:
    cols: 
      - id: "name"
        name: "Name"
        tip: "The name of the fruit."
      - id: "text"
        name: "Text"
        tip: "A brief description of the fruit."

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"

  # example of column alignment
  column_alignment:
    cols: 
      - id: "name"
        name: "Name"
        align: "left"
      - id: "text"
        name: "Text"
        align: "right"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"

  # example of table with cells as values
  cells_as_values:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"

  # example of table a single cell defined cells as a hash
  cells_as_hashes:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"
        align: "right"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: 
          # cell with custom alignment and tooltip
          val: "green"
          align: "left"
          tip: "This cell has a custom tooltip."

  # example of table with cells using markdown markup
  cell_markup_markdown:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    rows: 
      - name: "apple"
        text: "*red*" # italics
      - name: "banana"
        text: "**yellow**" # bold
      - name: "grape"
        text: "[green](https://pablotron.org/ \"my favorite site\")" # link

  # example of table with cells using html markup
  cell_markup_html:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    rows: 
      - name: "apple"
        # cell value using HTML markup for italics
        text: 
          val: "<i>red</i>"
          html: true
      - name: "banana"
        # cell value using HTML markup for bold
        text: 
          val: "<b>yellow</b>" # bold
          html: true
      - name: "grape"
        # cell value using HTML markup for a link
        text: 
          val: "<a href='https://pablotron.org/' title='my favorite site'>green</a>"
          html: true

  table_with_config_override:
    # table config override that renders right-aligned text as bold
    config:
      table_class: "table"
      align_left: "has-text-left"
      align_center: "has-text-centered"
      align_justify: "has-text-justified"
      align_right: "has-text-weight-bold"

    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"
        align: "right"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"

---

## Introduction

This [shortcode][] is a replacement for the lackluster table syntax in
the default [Hugo Markdown renderer][goldmark].

Features:
* Emits [CSS][] classes for alignment rather than inline `style`
  attributes (no more `unsafe-inline`).
* Table can be defined as [front matter][front matter] or [site data][].
* Column tooltips.
* Cell-specific overrides (alignment, tooltip, etc).
* [HTML][] and [Markdown][] markup support.
* Easy to configure emitted [CSS][] classes for a variety of frameworks,
  including [Bulma][] and [Bootstrap][].

[GitHub Repository][git]

## Quick Start

To get started:

1. Copy `table.html` from the [hugo-shortcode-table repository][git] to
   the `layouts/shortcodes` directory for your site.
2. Add a table definition to your [page front matter][front matter] (see
   below).
3. Add a `table` [shortcode][] to your page content which references the
   table definition from the previous step (see below).

Here is an example table definition for [YAML][] [front matter][]:

```yaml
tables:
  fruits:
    - ["name", "text"]
    - ["apple", "red"]
    - ["banana", "yellow"]
    - ["grape", "green"]
```
&nbsp;

Here is an example table declaration for your page content:

```md
{{</* table "fruits" */>}}
```
&nbsp;

This will render the following table:

{{< table "fruits" >}}

## Table Definitions

Tables can be defined as:

* An array of rows.
* A hash with `cols` and `rows` properties.

When defined as an array, the first row is used as the column headers.
Example:

```yaml
tables:
  fruits:
    - ["name", "text"] # <-- this row is used as the column headers
    - ["apple", "red"]
    - ["banana", "yellow"]
    - ["grape", "green"]
```
&nbsp;

When defined as a hash, the `cols` and `rows` properties are required.
Here is the same table defined as a hash:

```yaml
tables:
  fruits:
    # table columns
    cols: ["name", "text"]

    # table rows
    rows: 
      - ["apple", "red"]
      - ["banana", "yellow"]
      - ["grape", "green"]
```
&nbsp;

Defining a table as a hash allows you to specify several additional
table properties.  The [Table Properties section][table-props]
contains a complete list of available table properties.

### Table Data Sources

Table definitions can be stored in two places:

* [page front matter][front matter] in the `tables` section.
* [site `data` directory][site data] as [JSON][], [TOML][], or [YAML][]
  files.

**Note:** I will use [YAML][] front matter for the examples below.

#### Front Matter Tables

Here is an example of a table defined in the page [front matter][]:

##### Front Matter
```yaml
---
# ... other front matter omitted ...

tables:
  # example of table defined as arrays
  cols_as_array:
    # table columns (required)
    cols: ["name", "text"]

    # table rows (required)
    rows: 
      - ["apple", "red"]
      - ["banana", "yellow"]
      - ["grape", "green"]
---
```
&nbsp;

##### Content

```md
{{</* table "cols_as_array" */>}}
```
&nbsp;

##### Result

{{< table "cols_as_array" >}}

#### Site Data Tables

Tables can also be defined in the [site `data` directory][site data] by
using the two-argument form of the `table` [shortcode][].

Here is an example of the two argument form of of the `table`
shortcode:

```md
{{</* table "table_shortcode_examples" "fruits" */>}}
```
&nbsp;

In the example above, the `table` [shortcode][] will look for the table
data in the file `data/tables/table_shortcode_examples/fruits.yaml`.

##### Data File

Contents of `data/tables/table_shortcode_examples/fruits.yaml`.

```yaml
---
# table columns (required)
cols: ["name", "text"]

# table rows (required)
rows: 
  - ["apple", "red"]
  - ["banana", "yellow"]
  - ["grape", "green"]
```

##### Content

```md
{{</* table "table_shortcode_examples" "fruits" */>}}
```
&nbsp;

##### Result

{{< table "table_shortcode_examples" "fruits" >}}
&nbsp;

### Table Captions

This section contains an example of specifying a table with a `caption`
property.

#### Front Matter

Here is an example table definition with a `caption` property:

```yaml
---
# ... other front matter omitted ...

tables:
  table_caption:
    # table caption
    caption: "an example caption"

    # table columns
    cols: ["name", "text"]

    # table rows
    rows: 
      - ["apple", "red"]
      - ["banana", "yellow"]
      - ["grape", "green"]
---
```
&nbsp;

#### Content

Here is the corresponding table [shortcode][] from the page content:

```md
{{</* table "table_caption" */>}}
```
&nbsp;

#### Result

Here is the resulting table:

{{< table "table_caption" >}}

### Table Properties

Here is a complete list of available table properties:

{{< table "table_props" >}}

## Column Definitions

Table columns can be defined in two ways:

* As an array of values.
* As an array of hashes.

Defining columns as an array of hashes allows you to specify several
additional properties for each column.  The [Column Properties
section][col-props] contains a complete list of available column
properties.

### Columns Defined as an Array of Values

Here is an example of a table where the columns are defined as an array
of values.

#### Front Matter

Front matter for table with columns are defined with an array of values.

```yaml
---
# ... other front matter omitted ...

tables:
  # example of table with columns defined as an array of values
  cols_as_array:
    # table columns (required)
    cols: ["name", "text"]

    # table rows (required)
    rows: 
      - ["apple", "red"]
      - ["banana", "yellow"]
      - ["grape", "green"]
---
```
&nbsp;

#### Page Content

Here is the corresponding table [shortcode][] reference from the page
content for the table defined in the previous section:

```md
{{</* table "cols_as_array" */>}}
```
&nbsp;

#### Result

Here is the result:

{{< table "cols_as_array" >}}

### Columns Defined as an Array of Hashes

Here is an example of a table where the columns are defined as an array
of hashes.

#### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # example where columns are defined as hashes
  hashes:
    # table columns
    # the `id` property 
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    # table rows (required)
    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"
---
```
&nbsp;

#### Content

```md
{{</* table "hashes" */>}}
```
&nbsp;

#### Result

{{< table "hashes" >}}

### Column Tooltips

Use the `tip` property of columns to specify the `title` and
`aria-lable` of columns.

#### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  column_tooltips:
    # table columns (required)
    cols: 
      - id: "name"
        name: "Name"
        tip: "The name of the fruit."
      - id: "text"
        name: "Text"
        tip: "A brief description of the fruit."

    # table rows (required)
    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"
---
```
&nbsp;

#### Content

```md
{{</* table "column_tooltips" */>}}
```
&nbsp;

#### Result

{{< table "column_tooltips" >}}

**Hint:** Hover over the column headers and table cells to show the
custom tooltip.

### Column Alignment

Use the `align` property of columns to specify column alignment.

Valid values of the `align` property are:
* `left`
* `center`
* `justify`
* `right`

The rest of this section contains an example of a table with a
right-aligned second column.

#### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # table 
  column_alignment:
    # table columns (required)
    cols: 
      - id: "name"
        name: "Name"
        align: "left"
      - id: "text"
        name: "Text"
        align: "right"

    # table rows (required)
    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"
---
```
&nbsp;

#### Content

```md
{{</* table "column_alignment" */>}}
```
&nbsp;

#### Result

{{< table "column_alignment" >}}

### Column Properties

Here is a complete list of the available column properties:

{{< table "col_props" >}}

## Row Definitions

Rows are defined as:

* An array of values.
* Hashes.  This is **required** if columns are [defined as an
  array of hashes][cols-as-hashes].

Defining a rows with hashes allows you to specify several additional
properties.  The [Row Properties section][row-props] contains a complete
list of available properties.

The `<tr>` elements generated by this shortcode are annotated with
several additional [HTML][] attributes.  See the [Row HTML Attributes
section][row-attrs] for a complete list of [HTML][] attributes.

### Row Properties

Here is a complete list of available properties for rows defined as
hashes:

{{< table "row_props" >}}

### Row HTML Attributes

Generated `<tr>` elements for rows are annotated with the following
attributes:

{{< table "row_attrs" >}}

**Note:** For rows defined as an array of values, only the `data-tr_y`
attribute is generated.

## Cell Definitions

Row cells can be specified as:

* a value
* a hash

Defining cells with hashes allows you to specify several additional
properties.  The [Cell Properties section][cell-props] contains a
complete list of available properties.

The `<td>` elements generated by this shortcode for table cells are
annotated with several additional [HTML][] attributes.  See the [Cell
HTML Attributes section][cell-attrs] for a complete list of [HTML][]
attributes.

### Cells Defined as Values

Here is an example of a table with the cells defined as values.

#### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # example of table with cells as values
  cells_as_values:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"
        align: "right"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"
```
&nbsp;

#### Content

Here is the corresponding shortcode in the page content:

```md
{{</* table "cells_as_values" */>}}
```
&nbsp;

#### Result

Here is the generated table:

{{< table "cells_as_values" >}}

### Cells Defined as Hashes

Here is an example of a table with a single cell defined as a hash.

#### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # example of table a single cell defined cells as a hash
  cells_as_hashes:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"
        align: "right"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: 
          # cell with custom alignment and tooltip
          val: "green"
          align: "left"
          tip: "This cell has a custom tooltip."
```
&nbsp;

#### Content

Here is the corresponding shortcode in the page content:

```md
{{</* table "cells_as_hashes" */>}}
```
&nbsp;

#### Result

Here is the generated table:

{{< table "cells_as_hashes" >}}

**Hint:** Hover over the word `green` to see the custom tooltip.

### Cell Markup

Cell values can use the following markup:

* [Markdown][] (default)
* [HTML][], by specifying the cell as a hash and setting the `html`
  property to `true`.

#### Markdown

This section contains is an example table with a cell that uses
[Markdown][] markup.

**Note:** [Markdown][] links in cell values **cannot be specified**
using `[name][code]` syntax; instead they must use one of the following
[Markdown][] link syntaxes:

* `[name](full_url)`
* `[name](full_url "tooltip")`

##### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # example of table with cells using markdown markup
  cell_markup_markdown:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    rows: 
      - name: "apple"
        text: "*red*" # italics
      - name: "banana"
        text: "**yellow**" # bold
      - name: "grape"
        text: "[green](https://pablotron.org/ \"my favorite site\")" # link
```

##### Content

Here is the corresponding shortcode in the page content:

```md
{{</* table "cell_markup_markdown" */>}}
```
&nbsp;

##### Result

Here is the generated table:

{{< table "cell_markup_markdown" >}}

#### HTML

You can render cell values using [HTML][] markup by specifying the cell
as a hash and then setting the `html` property of the cell to `true`.

This section contains an example table containing cells using [HTML][]
markup.

##### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # example of table with cells using html markup
  cell_markup_html:
    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"

    rows: 
      - name: "apple"
        # cell value using HTML markup for italics
        text: 
          val: "<i>red</i>"
          html: true
      - name: "banana"
        # cell value using HTML markup for bold
        text: 
          val: "<b>yellow</b>" # bold
          html: true
      - name: "grape"
        # cell value using HTML markup for a link
        text: 
          val: "<a href='https://pablotron.org/' title='my favorite site'>green</a>"
          html: true
```
&nbsp;

##### Content

Here is the corresponding shortcode in the page content:

```md
{{</* table "cell_markup_html" */>}}
```
&nbsp;

##### Result

Here is the generated table:

{{< table "cell_markup_html" >}}

### Cell Properties

Here is a complete list of available properties for cells defined as
hashes:

{{< table "cell_props" >}}

### Cell HTML Attributes

Generated `<td>` elements for table cells are annotated with the
following attributes:

{{< table "cell_attrs" >}}

**Note:** For cells defined as a value rather than a hash, only
following attributes are generated for `<td>` elements:

* `data-td_x`
* `title` (set to the column name)
* `aria-label` (set to the column name)

## Table Configuration Overrides

Column and cell alignment is rendered using [CSS][] classes rather than
inline `style` attributes.

The default `align` to [CSS][] `class` mapping is as follows:

{{< table "align_classes" >}}

These classes work out of the box with [Bulma][].  If you're using
another framework, you can change the [CSS class][css]
configuration on a per-table, per-page, or per-site basis.

See the [Table Configuration Properties section][table-config-props]
for a full list of available table configuration properties.

### Table Override

Use the `config` table property to override the configuration for
a specific table.

#### Front Matter

```yaml
---
# ... other front matter omitted ...

tables:
  # table with custom config that renders right-aligned text as bold
  table_with_config_override:
    # table config override that renders right-aligned text as bold
    config:
      table_class: "table"
      align_left: "has-text-left"
      align_center: "has-text-centered"
      align_justify: "has-text-justified"
      align_right: "has-text-weight-bold"

    cols: 
      - id: "name"
        name: "Name"
      - id: "text"
        name: "Text"
        align: "right"

    rows: 
      - name: "apple"
        text: "red"
      - name: "banana"
        text: "yellow"
      - name: "grape"
        text: "green"
```
&nbsp;

#### Content

Here is the corresponding shortcode in the page content:

```md
{{</* table "table_with_config_override" */>}}
```
&nbsp;

#### Result

Here is the generated table:

{{< table "table_with_config_override" >}}

### Page Override

Use the `table_config` property in the [page front matter][front matter]
to override the configuration for all tables on a page that are
generated with the table [shortcode][].

Here is an example page-level table configuration override which
emits a `has-text-weight-bold` class for all table cells with an
alignment set to `justify`:

```yaml
---
# ... other front matter omitted ...

# override config for all tables generated with table shortcode on this
# page so that right-aligned cell values are rendered as bold
table_config:
  table_class: "table"
  align_left: "has-text-left"
  align_center: "has-text-centered"
  align_justify: "has-text-weight-bold"
  align_right: "has-text-right"
```
&nbsp;

### Site Override

Use the `table_config` parameter in the root `config.toml` to override
the table configuration for all tables across all pages on a site that
are generated with the table [shortcode][].

Here is an example overide for [Bootstrap][]:

```toml
# table shortcode bootstrap override
[params.table_config]
  # class to assign to all generated tables
  table_class = "table"

  # class for left-aligned column headers and cells
  align_left = "text-start"

  # class for centered column headers and cells
  align_center = "text-center"

  # class for justified column headers and cells
  # (note: doesn't provide a justify alignment class so you
  # would need to provide your own)
  align_justify = "my-custom-justify-class"

  # class for right-aligned column headers and cells
  align_right = "text-right"
```
&nbsp;

### Table Configuration Properties

Here is a full list of available table configuration properties:

{{< table "table_config_props" >}}

[site data]: https://gohugo.io/templates/data-templates/#the-data-folder
  "Site data directory."
[front matter]: https://gohugo.io/content-management/front-matter/
  "Hugo page metadata."
[css]: https://en.wikipedia.org/wiki/CSS
  "Cascading Style Sheets"
[bulma]: https://bulma.io/
  "Bulma CSS framework."
[bootstrap]: https://getbootstrap.com/
  "Bootstrap CSS framework."
[json]: https://json.org/
  "JavaScript Object Notation"
[yaml]: https://yaml.org/
  "YAML Ain't a Markup Language"
[toml]: https://github.com/toml-lang/toml
  "Tom's Obvious Markup Language"
[hugo]: https://gohugo.io/
  "Hugo static site generator"
[goldmark]: https://github.com/yuin/goldmark/
  "Goldmark Markdown renderer."
[html]: https://en.wikipedia.org/wiki/HTML
  "HyperText Markup Language"
[markdown]: https://en.wikipedia.org/wiki/Markdown
  "Markdown markup language"
[shortcode]: https://gohugo.io/content-management/shortcodes/
  "Hugo shortcode"
[git]: https://github.com/pablotron/hugo-shortcode-table
  "hugo-shortcode-table github repository"
[table-props]: #table-properties
  "Table Properties section"
[col-props]: #column-properties
  "Column Properties section"
[cols-as-hashes]: #columns-defined-as-an-array-of-hashes
  "Columns Defined as an Array of Hashes"
[row-props]: #row-properties
  "Row Properties section"
[row-attrs]: #row-html-attributes
  "Row HTML Attributes section"
[cell-props]: #cell-properties
  "Cell Properties section"
[cell-attrs]: #cell-html-attributes
  "Cell HTML Attributes section"
[table-config-props]: #table-configuration-properties
  "Table Configuration Properties section"