Vitrinev1.0.4

The example shortcode

A folder next to your page, one line in its Markdown, and the example runs.

A first example

Make the page a page bundle (a folder with an index.md) and put the example’s files in a folder next to it:

text
content/examples/counter/
├── index.md
└── demo/
    ├── example.html    required: the markup
    ├── style.css       optional
    └── script.js       optional

Then, in index.md:

md
{{< example "demo" >}}

This is the counter example, running (its files live in assets/examples/counter/, so this page and the Examples tab can share them; more below):

live · A counter
example.html
<button id="counter" type="button">Clicked <output>0</output> times</button>

The frame’s label says “live” and the example’s title. Under it, the files in tabs, with a copy button for the file shown.

Options

Pass the folder’s name alone, as above, or as name with other options:

OptionDefaultWhat it does
namenonethe folder with the example’s files
titlenonea title in the frame’s label
runtruefalse shows only the files
codetruefalse runs the example without showing its files
frametruefalse runs it bare: no frame, no files, its markup straight in the page
md
{{< example name="demo" title="Captions" >}}
{{< example name="demo" run="false" >}}
{{< example name="dialog" frame="false" >}}

A shortcode call takes either one name or named options, not both: {{< example "demo" title="x" >}} is an error.

Where the files can live

The shortcode looks for <name>/example.html in two places, in this order:

  1. next to the page, in its bundle: for examples that belong to one page;
  2. in your site’s assets/examples/<name>/: for examples several pages share, or examples on pages that aren’t bundles (the start page’s random walk lives in assets/examples/walk/).

Shared parts

Some examples need markup that isn’t part of what they demonstrate. The photo viewer needs one <dialog> per page. Put it in an example of its own and run it bare, with frame="false":

md
{{< example name="demo" title="Photo viewer" >}}

{{< example name="dialog" frame="false" >}}

The bare example’s markup, style and script go into the page without a frame or tabs. Write about it once, on a page of its own, and show its files there.

Paths in the markup

Write paths in example.html the way they’d look on your own site, from its root:

html
<img src="/media/photo-1-480.jpg" alt="…">

If your docs live under a subpath, say example.org/docs/, Vitrine adds it to every src, href and poster starting with / when the example runs. The files in the tabs keep the path as you wrote it.

What else to know