Concatinating Strings in Hugo
source link: https://blog.anantshri.info/concatinating-strings-in-hugo/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
A Quick tip around string concatination.
What doesnt works
{{ $a := $b + $c }}
{{ $a := $b $c }}
{{ $a := $b . $c }}
However what does work is
$a := printf "%s" $a | printf "%s"
However if you try to pipe multiple of these in one string like
$a := printf "%s" $a | printf "%s" $b | printf "%s"
This would add extra charcters
%!(EXTRA string=WORLD)
took me a while to figure out my own daftness
printf "%s" $a | printf "%s" $b | printf "%s"
This is akin to passing output from one command to another, the second command takes input from first commands output.
so a slightly tweaked version
printf "%s" $a | printf "%s%s" $b | printf "%s"
See that extra %s on second set. thats for the output coming from the first command.
Anyone trying to piece this together would realize the string has to be concatenated in inverted order.
so if I want $a + $b + $c
i will have to do it this way.
printf "%s" $c | printf "%s%s" $b | printf "%s%s" $a | printf "%s"
Remember we dont need to increase %s
as we move to left. You ask why, coz the previous command output has gotten squasted into a single string.
Thats it for today.
Note: The article was first published on now defunct : https://til.anantshri.info/post/hugo_string_concat/
Do you like what you read, What to share it
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Post navigation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK