XaminProject / handlebars.php

Handlebars processor for php
331 stars 132 forks source link

No way to generate \\{{var}} #113

Closed fzerorubigd closed 9 years ago

fzerorubigd commented 9 years ago

Correct result of (context is ['var' => 'value']) :

\\\{{var}}

is

\value

but handlebars.php produce

\\{{var}}
fzerorubigd commented 9 years ago

@JustBlackBird I can't get my head around this. I need to generate exactly one \ before a variable. handlebars.js use two \\ to output a \ but since its PHP, the the \\ in any case compiled to \ (before loading by engine, I can't understand why PHP translate '\\' to \ even when I am in single qute!) So if i want exactly two, I should have \\\ OR \\\\ (WTF?) so this are wrong https://github.com/XaminProject/handlebars.php/blob/master/tests/Xamin/HandlebarsTest.php#L130 Any idea on this?

JustBlackBird commented 9 years ago

@fzerorubigd, according to php manual \\\{ inside of single quotes is equal to \\\\{, thus this and this test cases do the same job.

To get a single backslash inside a PHP's string one should double it. So

$s = "\\{{var}}";

puts \{{var}} string into $s variable and

$s = "\\\\{{var}}";

puts \\{var}.

The problem is the current Handlebars.php parser does not replace \\ in a template with \. You can check this behavior by using templates with \Handlebars\Loader\FilesystemLoader:

\{{var}}

will be rendered to \{{var}} while

\\{{var}}

becomes \\value but should be rendered to \value.

To summarize, you are right, there is no way to render \\{{var}} template correctly.

majortom731 commented 9 years ago

A workaround would be to store a single backslash in a different variable and then use

{{backslash}}{{var}}

inside the template.

JustBlackBird commented 9 years ago

@majortom731 it's a dirty hack. The problem should be fixed in the Core.

fzerorubigd commented 9 years ago

@majortom731 Actually, I use that in my template right now. but as @JustBlackBird said we need to fix it in core.

JustBlackBird commented 9 years ago

Here is a jsfiddle that illustrates Handlebars.js behavior: http://jsfiddle.net/cLxyge73/1/ . It seems that only slashes before opening curly brace ({) should be doubled.

JustBlackBird commented 9 years ago

@fzerorubigd could you please review #118 ?