1

I have a configuration value that represents a folder where some files are available for download. This value is a handlebars template:

"D:\{{identifier1}}\{{identifier2}}\SomeFolder\{{version}}"

This value is set per environment by our deploy server.

The problem is that when I run the template through like this:

var template = Handlebars.Compile(_configuration.FilePathFormat);

        return template(new
        {
            identifier1 = 123,
            identifier2 = 456,
            version = "latest"
        });

The result I get back is this:

R:{{identifier1}}{{identifier2}}\SomeFolder{{version}}

What I expect:

R:\123\456\SomeFolder\latest

For some reason it is escaping the handlebars and I don't want that. I have been unable to find anything on disabling escape characters, and no amount of slashes has worked to render the template correctly.

Also, if I put a space between the slash and the handlebar placeholder, it compiles and renders correctly, but then it has a space in the file path.

I found a site that let me test handlebars.js, but it behaves differently than handlebars.net

2
  • 2
    This was a bug in Handlebars.Net that is now fixed. Commented May 2, 2018 at 2:12
  • Thank you Rex! I'll pull the latest version. Commented May 9, 2018 at 14:50

1 Answer 1

1

Update: Fixed in Handlebars.net

Sice version 1.9.3, released right after I reported the bug, this now works just fine. You have to double your slashes in the template to escape them for Handlebars.Net, same as for Handlebars.js,

var templateText = @"D:\\{{identifier1}}\\{{identifier2}}\\SomeFolder\\{{version}}";
var compiledTemplate = Handlebars.Compile(templateText);

var path = compiledTemplate(new
{
    identifier1 = 123,
    identifier2 = 456,
    version = "latest"
});

Original answer: Yes, it seems Handlebars.net has some weird handling of escape characters.

The simplest workaround is to just invert the slashes in the template string.

var templateText = @"D:/{{identifier1}}/{{identifier2}}/SomeFolder/{{version}}";
var compiledTemplate = Handlebars.Compile(templateText);

var pathWithInvertedSlashes = compiledTemplate(new
{
    identifier1 = 123,
    identifier2 = 456,
    version = "latest"
});
// Inverted slashes normally work fine on Windows, but if you want to
// 'fix' them you can always do
var canonicalPath = Path.GetFullPath(pathWithInvertedSlashes);
Sign up to request clarification or add additional context in comments.

3 Comments

I started down this path (forcing the \ to /), but was hoping I was missing something. Thanks for the help!
Handlebars.net has a fixed lookahead when it encounters a backslash. I reported a bug at github.com/rexm/Handlebars.Net/issues/247
This bug has been fixed in the latest version of Handlebars.Net. Thanks for raising it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.