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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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, Gutenberg 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.
  10. ## Writing a shortcode
  11. Let's write a shortcode to embed YouTube videos as an example.
  12. In a file called `youtube.html` in the `templates/shortcodes` directory, paste the
  13. following:
  14. ```jinja2
  15. <div {% if class %}class="{{class}}"{% endif %}>
  16. <iframe
  17. src="https://www.youtube.com/embed/{{id}}{% if autoplay %}?autoplay=1{% endif %}"
  18. webkitallowfullscreen
  19. mozallowfullscreen
  20. allowfullscreen>
  21. </iframe>
  22. </div>
  23. ```
  24. This template is very straightforward: an iframe pointing to the YouTube embed URL wrapped in a `<div>`.
  25. In terms of input, it expects at least one variable: `id`. Since the other variables
  26. are in a `if` statement, we can assume they are optional.
  27. That's it, Gutenberg will now recognise this template as a shortcode named `youtube` (the filename minus the `.html` extension).
  28. The markdown renderer will wrap an inline HTML node like `<a>` or `<span>` into a paragraph. If you want to disable that,
  29. simply wrap your shortcode in a `div`.
  30. ## Using shortcodes
  31. There are two kinds of shortcodes:
  32. - ones that do not take a body like the YouTube example above
  33. - ones that do, a quote for example
  34. In both cases, their arguments must be named and they will all be passed to the template.
  35. Lastly, a shortcode name (and thus the corresponding `.html` file) as well as the arguments name
  36. can only contain numbers, letters and underscores, or in Regex terms the following: `[0-9A-Za-z_]`.
  37. While theoretically an argument name could be a number, it will not be possible to use it in the template in that case.
  38. Argument values can be of 5 types:
  39. - string: surrounded by double quotes, single quotes or backticks
  40. - bool: `true` or `false`
  41. - float: a number with a `.` in it
  42. - integer: a number without a `.` in it
  43. - array: an array of any kind of values, except arrays
  44. Malformed values will be silently ignored.
  45. Both type of shortcodes will also get either a `page` or `section` variable depending on where they were used and a `config`
  46. one. Those values will overwrite any arguments passed to a shortcode so shortcodes should not use arguments called like one
  47. of these.
  48. ### Shortcodes without body
  49. Simply call the shortcode as if it was a Tera function in a variable block. All the examples below are valid
  50. calls of the YouTube shortcode.
  51. ```md
  52. Here is a YouTube video:
  53. {{/* youtube(id="dQw4w9WgXcQ") */}}
  54. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
  55. An inline {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}} shortcode
  56. ```
  57. Note that if you want to have some content that looks like a shortcode but not have Gutenberg try to render it,
  58. you will need to escape it by using `{{/*` and `*/}}` instead of `{{` and `}}`.
  59. ### Shortcodes with body
  60. For example, let's imagine we have the following shortcode `quote.html` template:
  61. ```jinja2
  62. <blockquote>
  63. {{ body }} <br>
  64. -- {{ author}}
  65. </blockquote>
  66. ```
  67. We could use it in our markup file like so:
  68. ```md
  69. As someone said:
  70. {%/* quote(author="Vincent") */%}
  71. A quote
  72. {%/* end */%}
  73. ```
  74. The body of the shortcode will be automatically passed down to the rendering context as the `body` variable and needs
  75. to be in a newline.
  76. If you want to have some content that looks like a shortcode but not have Gutenberg try to render it,
  77. you will need to escape it by using `{%/*` and `*/%}` instead of `{%` and `%}`. You won't need to escape
  78. anything else until the closing tag.
  79. ## Built-in shortcodes
  80. Gutenberg comes with a few built-in shortcodes. If you want to override a default shortcode template,
  81. simply place a `{shortcode_name}.html` file in the `templates/shortcodes` directory and Gutenberg will
  82. use that instead.
  83. ### YouTube
  84. Embed a responsive player for a YouTube video.
  85. The arguments are:
  86. - `id`: the video id (mandatory)
  87. - `class`: a class to add the `div` surrounding the iframe
  88. - `autoplay`: whether to autoplay the video on load
  89. Usage example:
  90. ```md
  91. {{/* youtube(id="dQw4w9WgXcQ") */}}
  92. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
  93. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}}
  94. ```
  95. Result example:
  96. {{ youtube(id="dQw4w9WgXcQ") }}
  97. ### Vimeo
  98. Embed a player for a Vimeo video.
  99. The arguments are:
  100. - `id`: the video id (mandatory)
  101. - `class`: a class to add the `div` surrounding the iframe
  102. Usage example:
  103. ```md
  104. {{/* vimeo(id="124313553") */}}
  105. {{/* vimeo(id="124313553", class="vimeo") */}}
  106. ```
  107. Result example:
  108. {{ vimeo(id="124313553") }}
  109. ### Streamable
  110. Embed a player for a Streamable video.
  111. The arguments are:
  112. - `id`: the video id (mandatory)
  113. - `class`: a class to add the `div` surrounding the iframe
  114. Usage example:
  115. ```md
  116. {{/* streamable(id="92ok4") */}}
  117. {{/* streamable(id="92ok4", class="streamble") */}}
  118. ```
  119. Result example:
  120. {{ streamable(id="92ok4") }}
  121. ### Gist
  122. Embed a [Github gist](https://gist.github.com).
  123. The arguments are:
  124. - `url`: the url to the gist (mandatory)
  125. - `file`: by default, the shortcode will pull every file from the URL unless a specific filename is requested
  126. - `class`: a class to add the `div` surrounding the iframe
  127. Usage example:
  128. ```md
  129. {{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") */}}
  130. {{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57", class="gist") */}}
  131. ```
  132. Result example:
  133. {{ gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") }}