Variables in string

I’m trying to do something like this:

{{if in .URL "/product/cars/(.Params.company | urlize)-(.Params.name | urlize)/"}}
...
{{end}}

And it doesn’t work as (.Params.company | urlize) and (.Params.name | urlize) are not evaluated but seen as strings. I’ve tried moving things around, but can’t get this construct to work. Suggestions?

ps. Sorry about the bad topic title. I wasn’t sure what to label this.

You can use printf to construct strings with dynamic content - in this case the insertion of some processed values.

This can be done as follows:

{{ printf "/product/cars/%s-%s/" (.Params.company | urlize) (.Params.name | urlize) }}

%s acts as a placeholder for the inserted string(s).

1 Like

Thanks.

I just managed to get it to work by doing this ugly thing:

{{$company := .Params.company | urlize}}
{{$title :=  .Params.name | urlize}}
{{$producturl := (print "/product/cars/" $company "-" $title "/")}}

But your solution looks much better.

Edit. Just tried your solution, but I get the following error

<urlize>: wrong number of args for urlize: want 1 got 0

Managed to just solve this error by changing the position of urlize like so:

{{ printf "/product/cars/%s-%s/" (urlize | Params.company) (urlize | .Params.name) }}

You’ve to pipe the variable .Params.name into urlize, not vice versa :slight_smile: