Shortcodes
On this page
Shortcodes embed reusable templates in content. The shortcode hook processes Content and Summary on Pages and Sections.
Enable
hooks: shortcode: enabled: true
Template Locations
Place shortcode templates at:
templates/shortcodes/{name}.htmltemplates/shortcodes/{name}.tpltemplates/shortcodes/{name}/index.htmltemplates/shortcodes/{name}/index.tpl
For example, create templates/shortcodes/bilibili.html:
<div class="shortcode-bilibili"> <iframe src="https://player.bilibili.com/player.html?bvid={{ params.id }}&page=1" scrolling="no" border="0" frameborder="no" allowfullscreen="true" > </iframe> </div>
Use in Content
The HTML form:
<shortcode bilibili id="BV1yB4cz8E9y" />
With a body:
<shortcode notice type="warning"> **Note:** This feature is under development. </shortcode>
When markups.markdown.directive_blocks: true, Markdown also supports:
:::shortcode notice type=info This **Markdown** is rendered and passed to the shortcode. - static-site - go :::
Directive blocks render Markdown to HTML before passing it as body. To preserve raw YAML, TOML, or other unparsed text, use <shortcode ...>...</shortcode> instead.
Template Variables
| Variable | Description |
|---|---|
params.{key} |
A supplied parameter |
params.Get("key") |
Read a parameter |
params.Pop("key") |
Read and remove a parameter, then forward the remaining attributes |
params.String() |
Render remaining parameters as consistently sorted HTML attributes |
body |
Content between the tags; nested shortcodes are rendered first |
name |
Shortcode name |
counter |
Occurrence number for this shortcode name in the current content, starting at zero |
current_lang |
Current language |
page |
Current Page, available in page content only |
section |
Current Section, available in section content only |
params.String() sorts attribute names and emits empty string values as boolean attributes:
{% set type = params.Pop("type") %} <div class="notice notice-{{ type }}" {{ params.String() }}> {{ body }} </div>
Behavior
- Shortcodes apply to Page and Section content and summaries.
- Nested shortcodes render from the inside out. Outer templates receive the inner shortcode's rendered HTML.
- Template execution errors produce a warning and preserve the original tags and body.
- An unclosed shortcode produces a warning and preserves the original content read so far.
Examples
YouTube
templates/shortcodes/youtube.html:
<div class="shortcode-youtube"> <iframe src="https://www.youtube.com/embed/{{ params.id }}" frameborder="0" allowfullscreen > </iframe> </div>
Usage:
<shortcode youtube id="dQw4w9WgXcQ" />
Code
templates/shortcodes/code.html:
{% if params.lang %} <pre><code class="language-{{ params.lang }}">{{ body }}</code></pre> {% else %} <pre><code>{{ body }}</code></pre> {% endif %}
Usage:
<shortcode code lang="python"> def hello(): print("Hello, Snow!") </shortcode>