Solved: Url forward slashes rendered quoted with backslash

Hi,

I get the following rendered wit a configurable URL used in a partial:

window.cookieconsent_options = {"message":"This website uses cookies.","dismiss":"Got it!","learnMore":"More info","link":"http:\/\/localhost:1313\/cookie-information\/","theme":"dark-top"};
As you can see the forward slashed in the URL get quoted by backslashes and I can’t figure out why this happens.

I define a parameter in config.toml:

`[params]

Set the “more info” URL in this parameter. You have to

provide a document page for this URL - see the example website

cookie_consent_info_url = “/cookie-information/”
`
In the base document (baseof.html) - at the moment located at layouts/_default to override the one from the theme - I do the following:

{{ if .Site.Params.cookie_consent_info_url }} {{ partial "cookie_consent.html" . }} {{ end }}
In layouts/partials/cookie_consent.html - at the moment not overriding anything in the theme - I have the following:

window.cookieconsent_options = {"message":"This website uses cookies.","dismiss":"Got it!","learnMore":"More info","link":"{{ .Site.Params.cookie_consent_info_url | absURL }}","theme":"dark-top"};

When I insert the parameter in a “single” document directly it renders to:

" /cookie-information "

The value surrounded by spaces and by quotes. The rendering through the partials template is shown at the top.

Any hint how to get rid of the quoting and why the rendering in the “single” content is so different?

Thanks,
Frank Tegtmeyer

1 Like

Go templates are context sensitive and escape under certain circumstances your variables. Have a look at the safeJS template function to mark your injected variables as save.

Unfortunately the “safeXX” functions don’t change the behaviour.

I tried
"link":"{{ .Site.Params.cookie_consent_info_url | safeJS }}"

and also

"link":"{{ printf "%s" .Site.Params.cookie_consent_info_url }}"

Interesting enough other URL variables render normal:

[[menu.sidebar]] name = "My 500px profile" url = "https://500px.com/fte368"

used in themes/hugo-theme-bootstrap4-blog/layouts/partials/sidebar.html
as
` {{ with .Site.Menus.sidebar }}

Links

    {{ range . }}
  1. {{ .Name }}
  2. {{ end }}
{{ end }} ` The only obvious difference for me is that the one partial is in my layouts folder and the other (working one) in the layout folder of the theme. But moving my partial to the theme doesn't help with the problem.

Regards,
Frank

Since you are constructing a json. could you try using jsonify here?

Documentation link to functions

This is a good workaround! Finally it works as expected.

I have now:

<script type="text/javascript"> window.cookieconsent_options = {{ dict "message" "This website uses cookies." "dismiss" "Got it!" "learnMore" "More info" "link" (.Site.Params.cookie_consent_info_url | absURL) "theme" "dark-top" | jsonify | safeJS }}; </script>

Many thanks to digitalcraftsman and sairam for answering my question!

If someone can explain why exactly the original version doesn’t work as expected this would be cool!

With kind regards,
Frank Tegtmeyer

1 Like