#1 2016-05-13 13:09:52

orclstemu
Member
Registered: 2016-05-13
Posts: 3

Mustache IF helper fix

I noticed the following test code didn't work:

mustache := TSynMustache.Parse( stringtoutf8('1+3={{tval}} - is it 4? {{#if tval=45}}yes!{{/if}}'));
s := utf8tostring(mustache.RenderJSON( stringtoutf8('{ "tval":45}'), nil, Mustache.HelpersGetStandardList ));
debug_alert(s);


.. turned out the problem was in SynMustache.pas on line 1221:

   valArr.InitArray([GetValueCopyFromContext(Copy(nam,1,j-1)),
            Copy(nam,j,k-j),GetValueCopyFromContext(Copy(nam,k+1,maxInt))],JSON_OPTIONS[true]);

needs to be:

   valArr.InitArray([GetValueCopyFromContext(Copy(nam,1,j-1)),
            Copy(nam,j,k-j),GetValueCopyFromContext(Copy(nam,k,maxInt))],JSON_OPTIONS[true]);


Thanks for the library! I use it together with DCEF3 to generate all kind of reports in my application!

Regards,
Stefan

Offline

#2 2016-05-13 13:37:35

orclstemu
Member
Registered: 2016-05-13
Posts: 3

Re: Mustache IF helper fix

... and line 1171 in TSynMustacheContextVariant.GetValueCopyFromContext :

  if (ValueName='') or (ValueName[1] in ['1'..'9','"','{','[']) or
     (ValueName='true') or (ValueName='false') or (ValueName='null') then

... need to include the "0" character :

  if (ValueName='') or (ValueName[1] in ['0'..'9','"','{','[']) or
     (ValueName='true') or (ValueName='false') or (ValueName='null') then


... or else "{{#if tval=0}} wouldn't work!

Offline

#3 2016-05-13 14:23:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Mustache IF helper fix

Please check http://synopse.info/fossil/info/435bfce8b3

I've included your fix, and added the corresponding regression tests.

Thanks for the feedback!

Offline

#4 2016-05-13 14:50:04

orclstemu
Member
Registered: 2016-05-13
Posts: 3

Re: Mustache IF helper fix

Looks good! Thank you Arnaud!

Offline

Board footer

Powered by FluxBB