Working with Mustache from amber

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Working with Mustache from amber

laci
Hi,
I wonder if anybody tried using Mustache from amber.
I am having difficulties with sections.

The following JavaScript code:
var data = {
  "beatles": [
    { "firstName": "John", "lastName": "Lennon" },
    { "firstName": "Paul", "lastName": "McCartney" },
    { "firstName": "George", "lastName": "Harrison" },
    { "firstName": "Ringo", "lastName": "Starr" }
  ],
  "name": function () {
    return this.firstName + " " + this.lastName;
  }
};
var template = '{{#beatles}} {{name}} {{/beatles}}';
var html = Mustache.render(template, data);
console.log(html);

Spits out "John Lennon  Paul McCartney  George Harrison  Ringo Starr"

The problem is defining the name function with amber.

Here is one working example:
        | data calc render html |
        calc := [5].
        data := #{'title' -> 'Joe'. 'calc' -> [5]}.
        data := #{'title' -> 'Joe'. 'calc' -> calc}.
        html := document Mustache render: '{{title}} spends ${{calc}}' value: data.  
        console log: html

How to define the name function for this example which is similar to the JavaScript version:
        | arr data template html |
        arr := Array new.
        arr add: #{'firstName' -> 'John'. 'lastName' -> 'Lennon'}.
        arr add: #{'firstName' -> 'Paul'. 'lastName' -> 'McCartney'}.
        data := #{'beatles' -> arr}.
        template = '{{#beatles}} {{name}} {{/beatles}}'.
        html := Mustache render: template value: data.  
        console log: html

This function gets called in loop for each array item.
Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Working with Mustache from amber

SebastianHC
Am 09.01.2014 14:29, schrieb laci:
> function () {
>      return this.firstName + " " + this.lastName;
>    }

I think this must be something like:

     at: 'name' put: [:thisArg :each| each.firstName + " " +
each.lastName ] currySelf


best is to search for "currySelf"


Sebastian

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Working with Mustache from amber

SebastianHC
In reply to this post by laci
Am 09.01.2014 14:29, schrieb laci:
> 'name' ->

'name' ->

instead of

at: 'name' put:


Sorry

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: Working with Mustache from amber

laci
Sebastian,
You are brilliant. This code does the trick.

        | arr data template html |
        arr := Array new.
        arr add: #{'firstName' -> 'John'. 'lastName' -> 'Lennon'}.
        arr add: #{'firstName' -> 'Paul'. 'lastName' -> 'McCartney'}.
        data := #{'beatles' -> arr}.
        "data at: 'name' put: [:thisArg :each | console log: thisArg.  each firstName, ' ', each lastName] currySelf."
        data at: 'name' put: [:each | (each at: 'firstName'), ' ', (each at: 'lastName')] currySelf.
        "console log: data."
        html := Mustache render: '{{#beatles}} {{name}} {{/beatles}}' value: data.  
        console log: html

What a community.
Cheers,
 laci