librelist archives

« back to archive

Nested sections/lists with same member/attribute name.

Nested sections/lists with same member/attribute name.

From:
Fernando Guillen
Date:
2010-08-20 @ 16:26
Hi people,

I'm new using MustacheRB template system.

I'm trying to render a nested hashes using the 'section' sintaxis, but
as there is not 'dot sintaxis' I can not difference to whom one
attribute belongs to.

The problem is that the upper hash has a member with the same name
than a member of the child. Example:

    data = {
      "mothers" => [
        { :name => 'Mother1', :children => [ {:name => "Kid1"}, {:name
=> "Kid2"} ] },
        { :name => 'Mother2', :children => [ {:name => "Kid1"}, {:name
=> "Kid2"} ] }
      ]
    }

In this example I try to go through the 'mothers' array and them
through the 'children' array of each 'mother' in that point the
{{name}} member I can't say if it is about the 'mother' or about the
'child'.

Maybe my code is more explanatory than my english:

* http://gist.github.com/540571

How can I resolve this situation?

Any suggestion is welcome.

Thanks

f.


-- 
Fernando Guillén
Freelance Web Developer
http://www.fernandoguillen.info
http://spainrb.org/fernando-guillen

Re: [mustache] Nested sections/lists with same member/attribute name.

From:
Rafael
Date:
2010-08-20 @ 22:39
> The problem is that the upper hash has a member with the same name
> than a member of the child.

It's is not possible to avoid naming clash like this in sections. Once
the parser finds a section which yields another context, the inner
context - the one visible inside that section - will be updated to
reflect new status. Otherwise, the parser would never know which one
it should refer to.

It's pretty much like any programming language behavior on blocks. You
can see all public variables of outer contexts from inside the inner
block you currently are until you declare a new variable with the same
lexeme.

The simpler and straight way to get around it is doing the right
thing: using different names. You also can simulate namespaces if you
feel like it, e.g.:

    Mustache.render <<-TEMPLATE, {:"name_mother" => "Mother",
:"name-child" => "son"}
    {{name_mother}} and {{name-child}}
    TEMPLATE


-- Rafael Valverde

Re: [mustache] Nested sections/lists with same member/attribute name.

From:
Rafael
Date:
2010-08-20 @ 23:07
Thinking more about it, I realize what you intended might be
accomplished by using dot notation. Directives are currently in
development, but you can check them out here:
http://github.com/defunkt/mustache/commits/pragmas

I wrote an example of what I had in mind but didn't test it:
http://gist.github.com/541369

Note that variable *name* is still clashing, although *person* is
accessible inside the section, and thus, so is *person.name*.

-- Rafael Valverde


On Fri, Aug 20, 2010 at 7:39 PM, Rafael <rafacvo@gmail.com> wrote:
>> The problem is that the upper hash has a member with the same name
>> than a member of the child.
>
> It's is not possible to avoid naming clash like this in sections. Once
> the parser finds a section which yields another context, the inner
> context - the one visible inside that section - will be updated to
> reflect new status. Otherwise, the parser would never know which one
> it should refer to.
>
> It's pretty much like any programming language behavior on blocks. You
> can see all public variables of outer contexts from inside the inner
> block you currently are until you declare a new variable with the same
> lexeme.
>
> The simpler and straight way to get around it is doing the right
> thing: using different names. You also can simulate namespaces if you
> feel like it, e.g.:
>
>    Mustache.render <<-TEMPLATE, {:"name_mother" => "Mother",
> :"name-child" => "son"}
>    {{name_mother}} and {{name-child}}
>    TEMPLATE
>
>
> -- Rafael Valverde
>

Re: [mustache] Nested sections/lists with same member/attribute name.

From:
Fernando Guillen
Date:
2010-08-21 @ 08:23
Thanks for the reply Rafael.

I have been trying with the 'pragmas' branch but not one of these
combinations have worked:

1:----------------
{{#mothers}}
{{name}}: {{#children}}{{name}}({{mothers.name}}) {{/children}}|
{{/mothers}}

(the mothers.name is rendered empty)

2:----------------
{{#mothers}}
{{name}}: {{#children}}{{name}}({{mother.name}}) {{/children}}|
{{/mothers}}

(the mother.name is rendered empty)

3:----------------
{{%DOT-NOTATION}}
{{#mothers}}
{{name}}: {{#children}}{{name}}({{mother.name}}) {{/children}}|
{{/mothers}}

(Mustache::UnknownPragma: Unknown Pragma: DOT-NOTATION)


Maybe the solution, for the time being, will be the one you propose:
changing the name of the attributes. Of course I can't modify the
models just to fit them into the Mustache syntaxis but maybe I can
transform them into the 'view' so the instance passed to the
'template' will be not the model but a wrapper with the name of the
attributes modified trying to avoid the attributes clash.

Any way.. this solution looks a bit weird for me :/. I think the
attributes clash can be very common.


Thanks a lot.

Regards

f.

-- 
Fernando Guillén
Freelance Web Developer
http://www.fernandoguillen.info
http://spainrb.org/fernando-guillen