Lego2012
6/6/2017 - 5:20 PM

Line Breaks in Shortcode

Line Breaks in Shortcode

Here's how to enter an image caption once and output it twice as an alt attribute and as an HTML element with line breaks.

Specify your line breaks in your img shortcode in every post.

Then in the Shortcode template call plainify to strip the HTML from the image's alt attribute. And finally use safeHTML to render the line breaks inside an element

Example Shortcode template:

<figure>
< img src="{{ $.Page.Site.BaseURL }}{{ .Get "src" }}" alt="{{ with .Get "caption"}}{{ . | plainify }}{{ end }}"/>
</figure>

<h4>{{ with .Get "caption"}}{{ . | safeHTML }}{{ end }}</h4>

Example Shortcode usage in .md files:

{{< img src="/images/mi3_800x398.jpg" caption="Untitled,<br> oil on canvas,<br> 142.5 x 70 cm, 2010" >}}