Liking cljdoc? Tell your friends :D

High-Level Table Examples Gallery

This gallery shows the high-level clj-string-layout.table API. It uses the same data for each named table format so the output shapes are easy to compare.

Use this API when you want common named table formats without writing layout DSL strings. For the lower-level clj-string-layout.core/layout API and custom DSL layouts, see the recipe book and layout language reference.

(require '[clj-string-layout.predicates :as pred]
         '[clj-string-layout.table :as table])

(def sample
  {:headers ["Name" "Qty" "Price"]
   :rows [["apple" 12 "$1.50"]
          ["pear" 4 "$2.00"]]})

The backing layout snippets below show the equivalent lower-level clj-string-layout.core/layout config for the sample data. Most examples use three left-aligned columns unless the section says otherwise. The high-level table API still handles headers, map rows, escaping, and column specs before rendering.

Plain

(table/table (assoc sample :format :plain))
Name   Qty  Price
apple  12   $1.50
pear   4    $2.00

Backing layout:

{:layout {:cols ["[L]  [L]  [L]"]}}

Markdown

(table/table (assoc sample :format :markdown))
| Name  | Qty | Price |
|:----- |:--- |:----- |
| apple | 12  | $1.50 |
| pear  | 4   | $2.00 |

Backing layout:

{:layout {:cols ["| [L] | [L] | [L] |"]
          :rows [["|:[-] |:[-] |:[-] |" :apply-for pred/second-row?]]}}

Markdown Alignment Formats

Use :markdown-left, :markdown-center, or :markdown-right when every column should use the same Markdown alignment. :markdown is the same as :markdown-left.

(table/table (assoc sample :format :markdown-center))
|  Name | Qty | Price |
|:-----:|:---:|:-----:|
| apple |  12 | $1.50 |
|  pear |  4  | $2.00 |
(table/table (assoc sample :format :markdown-right))
|  Name | Qty | Price |
| -----:| ---:| -----:|
| apple |  12 | $1.50 |
|  pear |   4 | $2.00 |

Backing layouts:

{:layout {:cols ["| [C] | [C] | [C] |"]
          :rows [["|:[-]:|:[-]:|:[-]:|" :apply-for pred/second-row?]]}}

{:layout {:cols ["| [R] | [R] | [R] |"]
          :rows [["| [-]:| [-]:| [-]:|" :apply-for pred/second-row?]]}}

ASCII Grid

(table/table (assoc sample :format :ascii-grid))
+-------+-----+-------+
| Name  | Qty | Price |
+-------+-----+-------+
| apple | 12  | $1.50 |
+-------+-----+-------+
| pear  | 4   | $2.00 |
+-------+-----+-------+

Backing layout:

{:layout {:cols ["| [L] | [L] | [L] |"]
          :rows [["+-[-]-+-[-]-+-[-]-+" :apply-for pred/all-rows?]]}}

Box

(table/table (assoc sample :format :box))
┌───────┬─────┬───────┐
│ Name  │ Qty │ Price │
├───────┼─────┼───────┤
│ apple │ 12  │ $1.50 │
├───────┼─────┼───────┤
│ pear  │ 4   │ $2.00 │
└───────┴─────┴───────┘

Backing layout:

{:layout {:cols ["│ [L] │ [L] │ [L] │"]
          :rows [["┌─[─]─┬─[─]─┬─[─]─┐" :apply-for pred/first-row?]
                 ["├─[─]─┼─[─]─┼─[─]─┤" :apply-for pred/interior-row?]
                 ["└─[─]─┴─[─]─┴─[─]─┘" :apply-for pred/last-row?]]}}

Aliases: :unicode-box and :ascii-box.

Double Box

(table/table (assoc sample :format :double-box))
╔═══════╦═════╦═══════╗
║ Name  ║ Qty ║ Price ║
╠═══════╬═════╬═══════╣
║ apple ║ 12  ║ $1.50 ║
╠═══════╬═════╬═══════╣
║ pear  ║ 4   ║ $2.00 ║
╚═══════╩═════╩═══════╝

Backing layout:

{:layout {:cols ["║ [L] ║ [L] ║ [L] ║"]
          :rows [["╔═[═]═╦═[═]═╦═[═]═╗" :apply-for pred/first-row?]
                 ["╠═[═]═╬═[═]═╬═[═]═╣" :apply-for pred/interior-row?]
                 ["╚═[═]═╩═[═]═╩═[═]═╝" :apply-for pred/last-row?]]}}

Aliases: :unicode-double-box and :ascii-double-box.

CSV

(table/table (assoc sample :format :csv))
Name,Qty,Price
apple,12,$1.50
pear,4,$2.00

Backing layout:

{:layout {:cols ["{[V]}{,[V]}" :repeat-for [pred/first-col? pred/not-first-col?]]}}

TSV

(table/table (assoc sample :format :tsv))
Name	Qty	Price
apple	12	$1.50
pear	4	$2.00

Backing layout:

{:layout {:cols ["{[V]}{\t[V]}" :repeat-for [pred/first-col? pred/not-first-col?]]}}

Pipe

(table/table (assoc sample :format :pipe))
Name|Qty|Price
apple|12|$1.50
pear|4|$2.00

Backing layout:

{:layout {:cols ["{[V]}{|[V]}" :repeat-for [pred/first-col? pred/not-first-col?]]}}

psql

(table/table (assoc sample :format :psql))
 Name   | Qty | Price
------+-----+------
 apple  | 12  | $1.50
 pear   | 4   | $2.00

Backing layout:

{:layout {:cols [" [L] { | [L]}" :repeat-for [pred/not-first-col?]]
          :rows [["[-]{-+-[-]}" :apply-for pred/second-row?]]}}

Org Mode

(table/table (assoc sample :format :org))
| Name  | Qty | Price |
|-----+---+-----|
| apple | 12  | $1.50 |
| pear  | 4   | $2.00 |

Backing layout:

{:layout {:cols ["{| [L] }{| [L] }|" :repeat-for [pred/first-col? pred/not-first-col?]]
          :rows [["{|[-]}{+[-]}|" :apply-for pred/second-row?]]}}

reStructuredText

(table/table (assoc sample :format :rst))
=====  ===  =====
Name   Qty  Price
=====  ===  =====
apple  12   $1.50
pear   4    $2.00
=====  ===  =====

Backing layout:

{:layout {:cols ["{[L]}{  [L]}" :repeat-for [pred/first-col? pred/not-first-col?]]
          :rows [["{[=]}{  [=]}" :apply-for pred/first-row?]
                 ["{[=]}{  [=]}" :apply-for pred/second-row?]
                 ["{[=]}{  [=]}" :apply-for pred/last-row?]]}}

HTML

(table/table (assoc sample :format :html))
<table>
  <tr><th>Name</th><th>Qty</th><th>Price</th></tr>
  <tr><td>apple</td><td>12</td><td>$1.50</td></tr>
  <tr><td>pear</td><td>4</td><td>$2.00</td></tr>
</table>

Backing renderer:

{:format :html
 :layout :html}

The high-level HTML format is rendered directly so it can emit <th> for the header row and <td> for data rows. Lower-level HTML presets are available when you want to render every row as data cells with the layout DSL.

Alignment

Column specs can align values independently of the selected format.

(table/table {:format :markdown
              :columns [{:key :name :title "Name"}
                        {:key :qty :title "Qty" :align :right}
                        {:key :price :title "Price" :align :right}]
              :rows [{:name "apple" :qty 12 :price "$1.50"}
                     {:name "pear" :qty 4 :price "$2.00"}]})
| Name  | Qty | Price |
|:----- | ---:| -----:|
| apple |  12 | $1.50 |
| pear  |   4 | $2.00 |

Backing layout for those column alignments:

{:layout {:cols ["| [L] | [R] | [R] |"]
          :rows [["|:[-] | [-]:| [-]:|" :apply-for pred/second-row?]]}}

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close