SOLVED: How to stop newline after shortcode output?

I have a VERY simply shortcode, “productname.html”, that refers to a config parameter:

{{.Site.Params.productname}}

When I write content containing “{{<productname>}}'s the best” the rendered HTML includes a trailing newline, which results in unwanted whitespace before my apostrophe. eg: Super Product 's the best

I have a work-around, but it’s very silly. I extend the shortcut so that it understands parameters,

{{.Site.Params.productname}}{{ if .Get 0}}{{index .Params 0}}{{ end }}

… and now I can write content like: “{{<productname> "'s"}} the best

Is there any way around this? Alternatively, is there a smarter way to make config parameters appear in content?

Thank you!

https://github.com/spf13/hugo/issues/1753

1 Like

Thank you for the pointer. I’m not sure this is expected behaviour, given practically every text editor on earth puts an EOL marker of some kind at the end of a line, but that’s fine. I can work around it.

I’m really enjoying getting to know Hugo.

Try the -}} syntax as template enddelimiter

Tantalising! Sadly, my search skills apparently are not up to the task. I can’t find any reference to defining a template enddelimiter (or end delimiter!)

1 Like

Found it here – and it works!

ie: My simple shortcode, “productname.html”, that refers to a config parameter now looks like this:

{{- .Site.Params.productname -}}

2 Likes

Perhaps you have any suggestion on how to fix my new line problem. Here is my shortcode:

{{if in .Page.Permalink “xap97”}}{{“http://docs.gigaspaces.com/api/JavaDoc9.7”}}
{{else if in .Page.Permalink “xap100”}}{{“http://docs.gigaspaces.com/api/JavaDoc10.0”}}
{{else if in .Page.Permalink “xap101”}}{{“http://docs.gigaspaces.com/api/JavaDoc10.1”}}
{{else if in .Page.Permalink “xap102”}}{{“http://docs.gigaspaces.com/api/JavaDoc10.2”}}
{{else if in .Page.Permalink “xap110”}}{{“http://docs.gigaspaces.com/api/JavaDoc11.0”}}
{{else if in .Page.Permalink “xap120”}}{{“http://docs.gigaspaces.com/api/JavaDoc12.0”}}
{{else}}{{“http://docs.gigaspaces.com/api/xap/12.1.0/java”}}{{end}}

If its the last case, then there is no newline generated, all other cases produce a newline.
If I put the entire statement on one line it does not produce any newlines.
In order to maintain my code I would like to keep it in the above form.

Any help would be greatly appreciated

I haven’t tested it, and am no Hugo expert, but I don’t see any evidence that you have tried the -}} syntax as discussed in this topic. Maybe give that a go :slight_smile:

1 Like

I am reviving this dead thread to try to put the answer more prominently for anyone searching.

Two solutions:

  1. :negative_squared_cross_mark: Turn off trailing newlines in your editor. (This is very error prone and subject to silent failures.)
  2. :white_check_mark: Make the last line of your shortcode file {{- "" -}}. (This works consistently.)

Full example:

{{- $x := .Get "x" -}} 
<mark class="{{ $x }}">
  {{- .Inner -}}
</mark>
{{- "" -}}
3 Likes