5

I am facing trouble in sending data from an azure web api having a legitimate backslash(\). Data field is user id which is of following pattern: Domain\UserId I want to store it in the database as it is. But Dot liquid doesn't process it. I tried using escape, escape_once and replace

{{ body.requestor | escape_once }}
{{ body.requestor | escape }}
{{ body.requestor | replace "\", "\\"}}

but none of them worked. I cant ask caller of my web api to pass the user id with two backslashes - \\. I have to make a change in my web api to accept the user id's as they are. Any inputs/pointers are appreciated.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Tappy
  • 95
  • 1
  • 10

4 Answers4

1

This may be a bug in dot Liquid implementation of escape standard filter, or arguably a point where the Shopify specification is too vague hence implementations will differ. dot Liquid is using .NET RegEx replace causing "\" for pattern matching to be interpreted as the beginning of the pattern and an escape per https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions

So you need {{ body.requestor | replace "\\", "\\"}} (!) The pattern to search for interprets the escape (so it's a single \ been matched) while the replacement string is not interpreting the escape (so it's the actual double \\ string).

David Burg
  • 1,064
  • 12
  • 14
0

Am I too late to the party? But here is the answer. First - Replace is case-sensitive. Then you need to use colon ":" fro parameters. And third, I have no explanation but I suspect that it takes item to find with escape and item to replace with, without escape. Here is the program

string templateString = 
@"Nothing: '{{ k3 }}'
Replace with dash: '{{ k3|Replace:""\\"", ""-"" }}'
Replace with double slash: '{{ k3|Replace:""\\"", ""\\"" }}'";

Template.NamingConvention = new CSharpNamingConvention();
var t = Template.Parse(templateString);
string output = t.Render(Hash.FromDictionary(new Dictionary<string, object>() {{ "k3", "Domain\\user" } }));
Console.WriteLine(output);

Output:

Nothing: 'Domain\user'
Replace with dash: 'Domain-user'
Replace with double slash: 'Domain\\user'

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Filters casing depend on the context in which you are using them. If you are using them on the online test site for instance the lower casing is correct. We can't assume the context from the question. – David Burg Jun 08 '20 at 21:21
0

This question appear when I'm looking for the same question, only for the ruby gem liquid implementation. David Burg's answer give me hint, and this is what works:

{{ "yes \ no" | replace "\", "\\\" }}

will replace the single backlash with double backlash

Taufiq Muhammadi
  • 352
  • 4
  • 12
0

You nearly got it, the correct solution is:

{{ body.requestor | replace "\", "\\\\"}}

One backslash for the first argument and four for the 2nd. The first argument is literal. The 2nd argument gets parsed and because we want two backslashes we need to escape each one of them with another backslash. Making a total of four backslashes.

K. Frank
  • 1,325
  • 1
  • 10
  • 19