Shortcodes
Shortcodes are utilities that help simplify adding pieces to your page and help us keep them consistent across all pages. Because shortcodes are processed separately from Markdown, they are only available in pages that begin with front matter.
Images
To add an image to your Handbook page, first put it somewhere in the _img
directory. Then, you can reference that image using a shortcode that will copy
it into the correct place in the output and provide a URL to that image. There
are two shortcodes, depending on your needs. Most commonly, you'll use the
image
shortcode:
{% image "_img/ocelot.png"
"ocelot" %}
The first part after image
is the path to the image and the second part is the
alt text. The example above will result in this image being displayed:
If you need to apply a CSS class to your image for styling purposes, you can
instead use the image_with_class
shortcode, like so:
{% image_with_class
"_img/ocelot.png"
"usa-input--error"
"alt text"
%}
Will result in this image being displayed:
Don't forget the alt text!
If your image adds context for sighted users, you should be sure to also include alt text that can be read aloud by screen readers. Alt text is not necessary if your image is purely decorative, such as a logo.
Linking to other Handbook pages
Because the Handbook can be deployed in multiple environments, we need to take
care with how we link between pages. Thankfully there's a shortcode to help! To
link to another page in the Handbook, simply use the page
shortcode with the
path to the other page. For example:
{% page "updating-the-handbook/components" %}
This shortcode will produce a URL that is suitable for use in links, like so:
[Handbook components]
({% page "updating-the-handbook/components" %})
Will produce this correct link: Handbook components
Linking to Slack channels
Rather than hunting down the exact link to a Slack channel, the slack_channel
shortcode enables you to quickly create a link to it based solely on the channel
name. By default, the link is rendered with the channel name, but you can
optionally set the link text to anything you want.
- {% slack_channel "tts-handbook" %}
- {% slack_channel
"tts-handbook"
"TTS Handbook community" %}
Produces this:
USWDS Icons
The , external,US Web Design System includes
, external,a set of icons that are
available for use in the Handbook. The uswds_icon
shortcode makes it nice and
easy to get them:
{% uswds_icon "campaign" %}
Produces the following icon:
Be careful with icons
Do not use icons in place of textual content. These icons do not come with alt text and are not accessible. Use them as decorative elements or as additional emphasis on the text.
Downloadable files
If you need to include a file for download in your Handbook page, you can use
the download
shortcode.
Arguments
- file path: The location of your file in the Handbook source code, relative
to the source code base directory. For convenience, there is already a
downloads
directory you can use.
Returns the URL to the downloadable file.
Example:
If you put a file called my-form.pdf
in the downloads
directory, you can
make that file available for download and get the URL to it this way:
[download link]
({% download
"downloads/my-form.pdf"
%})
Which would produce a link like this:
Other internal links
Sometimes you need to create links that may be elsewhere in the Handbook or
possibly external (for example, if you are iterating over a list of links
provided in a data file). In those cases, use the link
shortcode. This
shortcode does nothing if the provided link begins with http or https, but for
other cases, it modifies the link based on the Handbook's deployment
environment. For example:
- [link 1]({% link "about-us/code-of-conduct" %})
- [link 2]({% link "https://www.gsa.gov" %})
Produces:
Identifying the current page
The Handbook has a CSS class called usa-current
that can be used to identify
an element as being associated with the current page. To simplify figuring out
which element is "current," you can use the usa-current
shortcode. This is
primarily useful in navigation links where you are iterating over a list of URLs
and want to know which one corresponds to the current page. The short code
returns either the text usa-current
if the URL is current or empty text
otherwise, making it suitable to use as a class
attribute.
<a href="my-page" class="
{% usa-current list-item.path page.inputPath %}
">Some navigation link</a>
In this example, list-item
is presumably a single item from a list and it has
a path
property. page
is a variable provided by 11ty for each page, and
inputPath
is that page's location in the filesystem.