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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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: ones that do no take a body like the YouTube example above and ones that do, a quote for example.
  30. In both cases, their arguments must be named and they will all be passed to the template.
  31. Do note that shortcodes in code blocks will be ignored.
  32. ### Shortcodes without body
  33. Those look like rendering a variable in Tera.
  34. On a new line, call the shortcode as if it was a function in a variable block. All the examples below are valid
  35. calls of the YouTube shortcode.
  36. ```md
  37. {{ youtube(id="dQw4w9WgXcQ") }}
  38. {{ youtube(id="dQw4w9WgXcQ", autoplay=true) }}
  39. {{ youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") }}
  40. ```
  41. ### Shortcodes with body
  42. Those look like a block in Tera.
  43. For example, let's imagine we have the following shortcode `quote.html` template:
  44. ```jinja2
  45. <blockquote>
  46. {{ body }} <br>
  47. -- {{ author}}
  48. </blockquote>
  49. ```
  50. We could use it in our markup file like so:
  51. ```md
  52. {% quote(author="Vincent") %}
  53. A quote
  54. {% end %}
  55. ```
  56. The `body` variable used in the shortcode template will be implicitly passed down to the rendering
  57. context automatically.
  58. ## Built-in shortcodes
  59. Gutenberg comes with a few built-in shortcodes. If you want to override a default shortcode template,
  60. simply place a `{shortcode_name}.html` file in the `templates/shortcodes` directory and Gutenberg will
  61. use that instead.
  62. ### YouTube
  63. Embed a responsive player for a YouTube video.
  64. The arguments are:
  65. - `id`: the video id (mandatory)
  66. - `class`: a class to add the `div` surrounding the iframe
  67. - `autoplay`: whether to autoplay the video on load
  68. Usage example:
  69. ```md
  70. {{ youtube(id="dQw4w9WgXcQ") }}
  71. {{ youtube(id="dQw4w9WgXcQ", autoplay=true) }}
  72. {{ youtube(id="dQw4w9WgXcQ", autoplay=true, class="youtube") }}
  73. ```
  74. Result example:
  75. {{ youtube(id="dQw4w9WgXcQ") }}
  76. ### Vimeo
  77. Embed a player for a Vimeo video.
  78. The arguments are:
  79. - `id`: the video id (mandatory)
  80. - `class`: a class to add the `div` surrounding the iframe
  81. Usage example:
  82. ```md
  83. {{ vimeo(id="124313553") }}
  84. {{ vimeo(id="124313553", class="vimeo") }}
  85. ```
  86. Result example:
  87. {{ vimeo(id="124313553") }}
  88. ### Streamable
  89. Embed a player for a Streamable video.
  90. The arguments are:
  91. - `id`: the video id (mandatory)
  92. - `class`: a class to add the `div` surrounding the iframe
  93. Usage example:
  94. ```md
  95. {{ streamable(id="2zt0") }}
  96. {{ streamable(id="2zt0", class="streamble") }}
  97. ```
  98. Result example:
  99. {{ streamable(id="2zt0") }}
  100. ### Gist
  101. Embed a [Github gist]().
  102. The arguments are:
  103. - `url`: the url to the gist (mandatory)
  104. - `file`: by default, the shortcode will pull every file from the URL unless a specific filename is requested
  105. - `class`: a class to add the `div` surrounding the iframe
  106. Usage example:
  107. ```md
  108. {{ gist(id="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") }}
  109. {{ gist(id="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57", class="gist") }}
  110. ```
  111. Result example:
  112. {{ gist(url="https://gist.github.com/Keats/e5fb6aad409f28721c0ba14161644c57") }}