Shortcodes

Shortcodes are simple snippets that you can include in your markdown content files. VictorPy 0.1.32 will render it using a predefined template.

Performance – Shortcode processing is enabled by default. Thus, shortcodes have a performance cost when rendering/building your site: you can disable shortcodes processing by setting the site configuration parameter enable_shortcodes to False.

Shortcode usage syntax

You can include shortcodes in your content using the following syntax:

{% the_shortcode_name %}

Built-in shortcodes

VictorPy 0.1.32 ships with a set of predefined shortcodes:

sectionpages

Generates a list of links to the pages of current section (useful for section's index page).

Example

{% sectionpages %}

For example, this shortcode used in the current page outputs the following:


figure

Allow you to show complete figure as a one-liner.

Parameters

  • src: The address of the resource (may be something like "/static/images/my-image.jpg")
  • alt: An alternative text for the resource (think accessibility…)
  • caption: A descriptive text to show under the figure. If not provided, alt value will be used.

Example

{% figure src="//blog.lamaisondelamontagne.org/public/images/2011/Victor_Hugo_001.jpg" alt="Victor Hugo" %}

Victor Hugo

Figure – Victor Hugo


amazon

Generate ready-to-fly Amazon products links.

Parameters

Associate ID – The shortcode can include your Amazon associate ID in the links. Just make sure to have the amazon_id parameter defined in your configuration file.

Example

{% amazon image B072QZZDV7 %}

…outputs this:

Article


tweet

Display a tweet.

Parameters

  • id: The Twitter tweet id.

Example

{% tweet 860599072234844160 %}


alert […] endalert

To be documented.

row […] endrow

To be documented.

col […] endcol

To be documented.

pull […] endpull

To be documented.

User defined shortcodes

You can create your own shortcodes in order to extend existing VictorPy 0.1.32 features:

  1. Create a new file called shortcodes.py in your project directory.
  2. Add one function per shortcode to process (the function name is the shortcode name).
  3. Use your shortcode in whatever content page you want!

The context parameter

Every shortcode function gets a special context parameter, which contains the full context on the page using the shortcode.

shortcodes.py

def hello(context):
    return "Hello, we're on {page_title}!".format(page_title=context['page']['title'])

my-page.md

{% hello %}

Here is the result, when used on the current page:

Hello, we're on Shortcodes!

Extra parameters

You can specify other parameters to customize your shortcodes behaviour.

Let's say we want to create a shortcode to display the factorial of a number. We'd like to use it this way, with a custom parameters to get the number to factorialize:

The factorial of number 4 is {% factorial 4 %}.

Let's go ahead and create the factorial function in our shortcodes.py file:

shortcodes.py

def factorial(context, n):
    num = 1
    n = int(n)
    while n >= 1:
        num = num * n
        n = n - 1
    return str(num)

my-page.md

The factorial of number 4 is {% factorial 4 %}.

Here is the result:

The factorial of number 4 is 24.