Variables in string

I’m trying to do something like this:

{{if in .URL "/product/cars/( | urlize)-( | urlize)/"}}

And it doesn’t work as ( | urlize) and ( | 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/" ( | urlize) ( | urlize) }}

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

1 Like


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

{{$company := | urlize}}
{{$title := | 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 | (urlize | }}

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