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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. ## Using shortcodes
  29. There are two kinds of shortcodes:
  30. - ones that do not take a body like the YouTube example above
  31. - ones that do, a quote for example
  32. In both cases, their arguments must be named and they will all be passed to the template.
  33. Lastly, a shortcode name (and thus the corresponding `.html` file) as well as the arguments name
  34. can only contain numbers, letters and underscores, or in Regex terms the following: `[0-9A-Za-z_]`.
  35. While theoretically an argument name could be a number, it will not be possible to use it in the template in that case.
  36. Argument values can be of 5 types:
  37. - string: surrounded by double quotes, single quotes or backticks
  38. - bool: `true` or `false`
  39. - float: a number with a `.` in it
  40. - integer: a number without a `.` in it
  41. - array: an array of any kind of values, except arrays
  42. Malformed values will be silently ignored.
  43. ### Shortcodes without body
  44. Simply call the shortcode as if it was a Tera function in a variable block. All the examples below are valid
  45. calls of the YouTube shortcode.
  46. ```md
  47. Here is a YouTube video:
  48. {{/* youtube(id="dQw4w9WgXcQ") */}}
  49. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
  50. An inline {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}} shortcode
  51. ```
  52. Note that if you want to have some content that looks like a shortcode but not have Gutenberg try to render it,
  53. you will need to escape it by using `{{/*` and `*/}}` instead of `{{` and `}}`.
  54. ### Shortcodes with body
  55. For example, let's imagine we have the following shortcode `quote.html` template:
  56. ```jinja2
  57. <blockquote>
  58. {{ body }} <br>
  59. -- {{ author}}
  60. </blockquote>
  61. ```
  62. We could use it in our markup file like so:
  63. ```md
  64. As someone said:
  65. {%/* quote(author="Vincent") */%}
  66. A quote
  67. {%/* end */%}
  68. ```
  69. The body of the shortcode will be automatically passed down to the rendering context as the `body` variable and needs
  70. to be in a newline.
  71. If you want to have some content that looks like a shortcode but not have Gutenberg try to render it,
  72. you will need to escape it by using `{%/*` and `*/%}` instead of `{%` and `%}`. You won't need to escape
  73. anything else until the closing tag.
  74. ## Built-in shortcodes
  75. Gutenberg comes with a few built-in shortcodes. If you want to override a default shortcode template,
  76. simply place a `{shortcode_name}.html` file in the `templates/shortcodes` directory and Gutenberg will
  77. use that instead.
  78. ### YouTube
  79. Embed a responsive player for a YouTube video.
  80. The arguments are:
  81. - `id`: the video id (mandatory)
  82. - `class`: a class to add the `div` surrounding the iframe
  83. - `autoplay`: whether to autoplay the video on load
  84. Usage example:
  85. ```md
  86. {{/* youtube(id="dQw4w9WgXcQ") */}}
  87. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true) */}}
  88. {{/* youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") */}}
  89. ```
  90. Result example:
  91. {{ youtube(id="dQw4w9WgXcQ") }}
  92. ### Vimeo
  93. Embed a player for a Vimeo video.
  94. The arguments are:
  95. - `id`: the video id (mandatory)
  96. - `class`: a class to add the `div` surrounding the iframe
  97. Usage example:
  98. ```md
  99. {{/* vimeo(id="124313553") */}}
  100. {{/* vimeo(id="124313553", class="vimeo") */}}
  101. ```
  102. Result example:
  103. {{ vimeo(id="124313553") }}
  104. ### Streamable
  105. Embed a player for a Streamable video.
  106. The arguments are:
  107. - `id`: the video id (mandatory)
  108. - `class`: a class to add the `div` surrounding the iframe
  109. Usage example:
  110. ```md
  111. {{/* streamable(id="92ok4") */}}
  112. {{/* streamable(id="92ok4", class="streamble") */}}
  113. ```
  114. Result example:
  115. {{ streamable(id="92ok4") }}
  116. ### Gist
  117. Embed a [Github gist](https://gist.github.com).
  118. The arguments are:
  119. - `url`: the url to the gist (mandatory)
  120. - `file`: by default, the shortcode will pull every file from the URL unless a specific filename is requested
  121. - `class`: a class to add the `div` surrounding the iframe
  122. Usage example:
  123. ```md
  124. {{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") */}}
  125. {{/* gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57", class="gist") */}}
  126. ```
  127. Result example:
  128. {{ gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") }}