#1 2016-11-12 15:51:13

Junior/RO
Member
Registered: 2011-05-13
Posts: 210

Possible bug in SynMustache: rendering top level arrays

I have tried to render a simple array with SynMustache

json := '["a","b","c"]';
mustache := TSynMustache.Parse('<ul>{{#.}}<li>{{.}}</li>{{/.}}</ul>');
html := mustache.RenderJSON(json);

But I can't, because SynMustache don't render this template. I think this is a bug. Please see this entry in Stackoverflow.

Offline

#2 2016-11-12 17:42:10

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Possible bug in SynMustache: rendering top level arrays

Yes, this is bug. JavaScript version did render as expected.

Offline

#3 2016-11-23 22:47:28

Junior/RO
Member
Registered: 2011-05-13
Posts: 210

Re: Possible bug in SynMustache: rendering top level arrays

Offline

#4 2016-11-24 07:53:12

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

Re: Possible bug in SynMustache: rendering top level arrays

There is an easy workaround to this limitation, for sure, which is to define a property containing the array.
Even if it is rendered by the JavaScript renderer, it is not documented as such.

There is even no mention of {{.}} in https://mustache.github.io/mustache.5.html
And the official specification files does not test a top level array, but only the cases supported by SynMustache:

  # Implicit Iterators

  - name: Implicit Iterator - String
    desc: Implicit iterators should directly interpolate strings.
    data:
      list: [ 'a', 'b', 'c', 'd', 'e' ]
    template: '"{{#list}}({{.}}){{/list}}"'
    expected: '"(a)(b)(c)(d)(e)"'

  - name: Implicit Iterator - Integer
    desc: Implicit iterators should cast integers to strings and interpolate.
    data:
      list: [ 1, 2, 3, 4, 5 ]
    template: '"{{#list}}({{.}}){{/list}}"'
    expected: '"(1)(2)(3)(4)(5)"'

  - name: Implicit Iterator - Decimal
    desc: Implicit iterators should cast decimals to strings and interpolate.
    data:
      list: [ 1.10, 2.20, 3.30, 4.40, 5.50 ]
    template: '"{{#list}}({{.}}){{/list}}"'
    expected: '"(1.1)(2.2)(3.3)(4.4)(5.5)"'

  - name: Implicit Iterator - Array
    desc: Implicit iterators should allow iterating over nested arrays.
    data:
      list: [ [1, 2, 3], ['a', 'b', 'c'] ]
    template: '"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}"'
    expected: '"(123)(abc)"'

See https://github.com/mustache/spec/blob/m … ctions.yml

So IMHO it is not a bug, but a limitation to an undocumented feature.

Offline

#5 2016-11-24 19:22:45

Junior/RO
Member
Registered: 2011-05-13
Posts: 210

Re: Possible bug in SynMustache: rendering top level arrays

@ab, I agree with you. I haven't read the official specification, only StackOverflow. This is a undocumented feature.

Offline

#6 2022-07-18 13:46:42

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Possible bug in SynMustache: rendering top level arrays

While implementing a TFB benchmark I found they use a top-level array in template (not a problem - I modify a template - it's allowed).
I also sometimes use a top level array in my templates rendered by JS.
So this is common feature and may be can be implementer in future.

Offline

#7 2022-07-28 17:04:49

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Possible bug in SynMustache: rendering top level arrays

Starting from  https://github.com/synopse/mORMot2/commit/fed0021366c top level arrays is supported

Offline

Board footer

Powered by FluxBB