I am new to Hugo as well as to Go lang. I am trying to create a paired shortcode which would split {{ .Inner }}
into lines and add a prefix to each line. To make it simple, I’d like this:
{{< prompt role="user" symbol="$" >}}
line 1
line 2
{{< /prompt >}}
to be converted to this:
<pre>
user $ line 1
user $ line 2
</pre>
I created theme/layouts/shortcodes/prompt.html
. Leaving aside all fancy stuff, it contains:
<pre>
{{- .Get "role" }} {{ .Get "symbol" -}}
{{- .Inner -}}
</pre>
The result is pretty close to what I want but I need to split {{ .Inner }}
into lines. I learned that I can use {{ .Inner }}
in Hugo and there is a Split()
function in strings
package in Go. As far as I understand, I can’t import this package directly in Hugo. How can I then achieve my goal?
Use a shortcode with markdown…which can be called as {{%%}}
instead of {{<>}}
. The percent sign tells Hugo to put .Inner through BkackFriday.
Thanks for the suggestion, but it won’t help in my case. First, the shortcode is supposed to work with code blocks which I don’t want to be processed as markdown. Second, it still gives me no possibility to access to {{ .Inner }}
line by line and to add a prefix to each line.
Can you use split
?
{{ range := split .Inner "\n" }}
prefix {{ printf "%s\n" . }}
{{ end }}
1 Like
Gotcha. I’m still not 100% exactly how you want the desired output, but if the first and second arguments are always paired (i.e., user
always equals $
, or any other combination), you can do this with multiple combinations on the client (namely, pseudo selectors but maybe a bit of JS). I’ve been fiddling with code block shortcodes a lot lately. LMK if I can help.
Exactly what I needed! Thanks! Actually I was trying to do that but apparently my syntax was wrong. Though yours example also contains odd :=
after range
. That worked for me:
{{ range split .Inner "\n" }}
prefix {{ printf "%s\n" . }}
{{ end }}
Sorry that I wasn’t very clear. Finally it came out that my question was about the syntax. Thanks for interesting pointers anyways!
1 Like