You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shortcodes.md 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. +++
  2. title = "Shortcodes"
  3. weight = 40
  4. +++
  5. Although Markdown is good for writing, it isn't great when you need write inline
  6. HTML to add some styling for example.
  7. To solve this, Zola borrows the concept of [shortcodes](https://codex.wordpress.org/Shortcode_API)
  8. from WordPress.
  9. In our case, a shortcode corresponds to a template defined in the `templates/shortcodes` directory or
  10. a built-in one that can be used in a Markdown file. If you want to use something similar to shortcodes in your templates, try [Tera macros](https://tera.netlify.com/docs/templates/#macros).
  11. ## Writing a shortcode
  12. Let's write a shortcode to embed YouTube videos as an example.
  13. In a file called `youtube.html` in the `templates/shortcodes` directory, paste the
  14. following:
  15. ```jinja2
  16. <div {% if class %}class="{{class}}"{% endif %}>
  17. <iframe
  18. src="https://www.youtube.com/embed/{{id}}{% if autoplay %}?autoplay=1{% endif %}"
  19. webkitallowfullscreen
  20. mozallowfullscreen
  21. allowfullscreen>
  22. </iframe>
  23. </div>
  24. ```
  25. This template is very straightforward: an iframe pointing to the YouTube embed URL wrapped in a `<div>`.
  26. In terms of input, this shortcode expects at least one variable: `id`. Because the other variables
  27. are in an `if` statement, they are optional.
  28. That's it. Zola will now recognise this template as a shortcode named `youtube` (the filename minus the `.html` extension).
  29. The Markdown renderer will wrap an inline HTML node such as `<a>` or `<span>` into a paragraph.
  30. If you want to disable this behaviour, wrap your shortcode in a `<div>`.
  31. Shortcodes are rendered before the Markdown is parsed so they don't have access to the table of contents. Because of that,
  32. you also cannot use the `get_page`/`get_section`/`get_taxonomy` global functions. It might work while running
  33. `zola serve` because it has been loaded but it will fail during `zola build`.
  34. ## Using shortcodes
  35. There are two kinds of shortcodes:
  36. - ones that do not take a body, such as the YouTube example above
  37. - ones that do, such as one that styles a quote
  38. In both cases, the arguments must be named and they will all be passed to the template.
  39. Lastly, a shortcode name (and thus the corresponding `.html` file) as well as the argument names
  40. can only contain numbers, letters and underscores, or in Regex terms `[0-9A-Za-z_]`.
  41. Although theoretically an argument name could be a number, it will not be possible to use such an argument in the template.
  42. Argument values can be of one of five types:
  43. - string: surrounded by double quotes, single quotes or backticks
  44. - bool: `true` or `false`
  45. - float: a number with a decimal point (e.g., 1.2)
  46. - integer: a whole number or its negative counterpart (e.g., 3)
  47. - array: an array of any kind of value, except arrays
  48. Malformed values will be silently ignored.
  49. Both types of shortcode will also get either a `page` or `section` variable depending on where they were used
  50. and a `config` variable. These values will overwrite any arguments passed to a shortcode so these variable names
  51. should not be used as argument names in shortcodes.
  52. ### Shortcodes without body
  53. Simply call the shortcode as if it was a Tera function in a variable block. All the examples below are valid
  54. calls of the YouTube shortcode.
  55. ```md
  56. Here is a YouTube video:
  57. {{/* youtube(id="dQw4w9WgXcQ") */}}
  58. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
  59. An inline {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}} shortcode
  60. ```
  61. Note that if you want to have some content that looks like a shortcode but not have Zola try to render it,
  62. you will need to escape it by using `{{/*` and `*/}}` instead of `{{` and `}}`.
  63. ### Shortcodes with body
  64. Let's imagine that we have the following shortcode `quote.html` template:
  65. ```jinja2
  66. <blockquote>
  67. {{ body }} <br>
  68. -- {{ author}}
  69. </blockquote>
  70. ```
  71. We could use it in our Markdown file like so:
  72. ```md
  73. As someone said:
  74. {%/* quote(author="Vincent") */%}
  75. A quote
  76. {%/* end */%}
  77. ```
  78. The body of the shortcode will be automatically passed down to the rendering context as the `body` variable and needs
  79. to be on a new line.
  80. If you want to have some content that looks like a shortcode but not have Zola try to render it,
  81. you will need to escape it by using `{%/*` and `*/%}` instead of `{%` and `%}`. You won't need to escape
  82. anything else until the closing tag.
  83. ## Built-in shortcodes
  84. Zola comes with a few built-in shortcodes. If you want to override a default shortcode template,
  85. simply place a `{shortcode_name}.html` file in the `templates/shortcodes` directory and Zola will
  86. use that instead.
  87. ### YouTube
  88. Embed a responsive player for a YouTube video.
  89. The arguments are:
  90. - `id`: the video id (mandatory)
  91. - `class`: a class to add to the `<div>` surrounding the iframe
  92. - `autoplay`: when set to "true", the video autoplays on load
  93. Usage example:
  94. ```md
  95. {{/* youtube(id="dQw4w9WgXcQ") */}}
  96. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
  97. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}}
  98. ```
  99. Result example:
  100. {{ youtube(id="dQw4w9WgXcQ") }}
  101. ### Vimeo
  102. Embed a player for a Vimeo video.
  103. The arguments are:
  104. - `id`: the video id (mandatory)
  105. - `class`: a class to add to the `<div>` surrounding the iframe
  106. Usage example:
  107. ```md
  108. {{/* vimeo(id="124313553") */}}
  109. {{/* vimeo(id="124313553", class="vimeo") */}}
  110. ```
  111. Result example:
  112. {{ vimeo(id="124313553") }}
  113. ### Streamable
  114. Embed a player for a Streamable video.
  115. The arguments are:
  116. - `id`: the video id (mandatory)
  117. - `class`: a class to add to the `<div>` surrounding the iframe
  118. Usage example:
  119. ```md
  120. {{/* streamable(id="92ok4") */}}
  121. {{/* streamable(id="92ok4", class="streamble") */}}
  122. ```
  123. Result example:
  124. {{ streamable(id="92ok4") }}
  125. ### Gist
  126. Embed a [Github gist](https://gist.github.com).
  127. The arguments are:
  128. - `url`: the url to the gist (mandatory)
  129. - `file`: by default, the shortcode will pull every file from the URL unless a specific filename is requested
  130. - `class`: a class to add to the `<div>` surrounding the iframe
  131. Usage example:
  132. ```md
  133. {{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") */}}
  134. {{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57", class="gist") */}}
  135. ```
  136. Result example:
  137. {{ gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") }}