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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. +++
  2. title = "Shortcodes"
  3. weight = 40
  4. +++
  5. While Markdown is good at 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, the shortcode corresponds to a template that is defined in the `templates/shortcodes` directory or a built-in one that can
  10. 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, it expects at least one variable: `id`. Since the other variables
  27. are in a `if` statement, we can assume 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 like `<a>` or `<span>` into a paragraph. If you want to disable that,
  30. simply wrap your shortcode in a `div`.
  31. Shortcodes are rendered before parsing the markdown so it doesn't have access to the table of contents. Because of that,
  32. you also cannot use the `get_page`/`get_section`/`get_taxonomy` global function. It might work while running `zola serve` because
  33. 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 like the YouTube example above
  37. - ones that do, a quote for example
  38. In both cases, their 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 arguments name
  40. can only contain numbers, letters and underscores, or in Regex terms the following: `[0-9A-Za-z_]`.
  41. While theoretically an argument name could be a number, it will not be possible to use it in the template in that case.
  42. Argument values can be of 5 types:
  43. - string: surrounded by double quotes, single quotes or backticks
  44. - bool: `true` or `false`
  45. - float: a number with a `.` in it
  46. - integer: a number without a `.` in it
  47. - array: an array of any kind of values, except arrays
  48. Malformed values will be silently ignored.
  49. Both type of shortcodes will also get either a `page` or `section` variable depending on where they were used and a `config`
  50. one. Those values will overwrite any arguments passed to a shortcode so shortcodes should not use arguments called like one
  51. of these.
  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. For example, let's imagine 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 markup 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 in a newline.
  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 the `div` surrounding the iframe
  92. - `autoplay`: whether to autoplay the video 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 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 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 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") }}