Escaping Hugo shortcodes

September 2018 · 2 minute read

In the rare case you might need to escape (i.e., prevent from executing) a shortcode in a Hugo markdown (.md) file, and display the shortcode as {{< myshortcode >}} you will probably find that escaping the double curly braces with \ is not working for this, producing a result similar to this \{{< myshortcode >\}} or this \{\{< myshortcode >\}\} or that the shortcode is executed nevertheless.

A simple method that works at the time of writing, is adding /* after the opening double curly braces and the angle bracket or percent sign (i.e., {{</* or {{%/*) and adding */ after the closing angle bracket or percent sign and double curly braces (i.e., */>}} or */%}}). So, if for example, you have the shortcode myshortcode and you want to use it to a code highlight without being executed during page building from the Hugo engine, you should try including it in your markdown file as:

{{</* myshortcode */>}}

The above will produce an output like the following:

{{< myshortcode >}}

Enjoy coding!

References for further reading:

  1. How is the Hugo Doc site showing shortcodes in code blocks?
  2. How To Escape Shortcode In Hugo Template
Above opinions and any mistakes are my own. I am not affiliated in any way with companies, or organizations mentioned above. The code samples provided are licensed under the Apache 2.0 License and rest content of this page is licensed under the Creative Commons Attribution 3.0 License, except if noted otherwise.