Escaping backslashes in configuration templates

Hi everyone -

I’m attempting to use the strReplace helper function within a configuration template to replace a backslash \ character with a forward slash / in a path. In my template, I’ve tried the lines of code below:

  • {{strReplace pkg.path “\\” “/”}}
  • {{strReplace pkg.path ‘\\’ “/”}}
  • {{strReplace pkg.path ‘\’ “/”}}
  • {{strReplace pkg.path “\” “/”}}

Every time I try to run my package, it fails with a template syntax error similar to what is shown below:
components\sup\src\manager\service\config.rs: TemplateError(TemplateError { reason: InvalidSyntax, template_name: Some(“my_file.config”), line_no: Some(47), column_no: Some(93) })

If I try the following line of code, though, it will work as expected and replace a value within the pkg.path string:

  • {{strReplace pkg.path “test” “test2”}}

Does anyone know how to properly escape a backslash using the strReplace helper?

Habitat uses handlebars for its templating language. I was just reading through https://handlebarsjs.com/expressions.html regarding helper functions, and it sounds like each parameter to a helper function needs to be a valid handlebars expression. Both forward and back slashes are not legal handlebars identifiers by themselves, but there is segment-literal notation when you have identifiers that contain illegal characters.

Try this:

{{strReplace pkg.path [\] [/]}}

and see if that works.

If I understand the documentation correctly, it looks like the square brackets [ ] are used only when accessing properties which keys have special characters in them – looks like it’s not used for arguments passed to a helper method. I tried the {{strReplace pkg.path [\] [/]}} and it still resulted in a syntax error.

After reading through the documentation some more, it looks to me like handlebars doesn’t support anything other than simple values (which handlebars describes as “simple String, number, or boolean”) as parameters to handlebars helpers. Specifically, I’m referring to the “Helpers” section of https://handlebarsjs.com/expressions.html.

You can also experiment with different things here http://tryhandlebarsjs.com but from what I can tell, this is a limitation of handlebars itself.

Thanks - I’ll mess around but it sounds like there isn’t anyway to get around this limitation today. Would this be something that should be opened as an issue to the Habitat dev team?

In my opinion, it’d be better raised with the Handlebars dev team, since we just use the handlebars library. If/when handlebars supports this, it should automatically work in Habitat.