Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

templates - Mustache (pystache) named context in list

I'm trying to render a list of values. In my real problem, each list item has a name property, and also another list, in which the items also have a 'name' property. I'm trying to avoid renaming the nested list items in my context.

I've done some testing and I figure out that this works perfectly:

        template = '''
{{#my_list}}
My list name is: {{my_list.name}}
{{/my_list}}
'''
        
        context = {'my_list' : {'name': 'This is my first item'}}

        renderer = pystache.Renderer(missing_tags='strict')
        s = renderer.render(template, context)
        print(s)

The output is what you expect:

My list name is: This is my first item

However, if my_list is actually a list (like it is in my actual problem), this breaks:

        context = {'my_list' : [
                {'name': 'This is my first item'},
                {'name': 'This is my second item'}
            ]}

I get a pystache.context.KeyNotFoundError: Key 'my_list.name' not found: missing 'name'.

My goal with this is to try to solve this problem:

template = '''
{{#my_list}}
{{#sublist}}
{{name}} from {{name}}
{{/sublist}}
{{/my_list}}
'''

context = {'my_list' : [
        {'name': 'list first item',
         'sublist' : [
             {'name': 'sublist first item'},
             {'name': 'sublist second item'}
          ]},
        {'name': 'list second item',
         'sublist' : [
             {'name': 'sublist first item'},
             {'name': 'sublist second item'}
          ]}
    ]}

And I want this rendered:

sublist first item from list first item
sublist second item from list first item
sublist first item from list second item
sublist second item from list second item

But I get this rendered:

sublist first item from sublist first item
sublist second item from sublist second item
list second item from list second item
list second item from list second item

I thought that having a template with named elements would solve my problem:

template = '''
{{#my_list}}
{{#sublist}}
{{sublist.name}} from {{my_list.name}}
{{/sublist}}
{{/my_list}}
'''

There's a solution for Handlebars, but it won't work for Mustache.

Edit:

I've also tried with Chevron, it doesn't work.

Edit 2:

I managed to tweak the context and the template to 'make it work':

template = '''
{{#my_list}}
{{#my_list_item.sublist}}
{{name}} from {{my_list_item.name}}
{{/my_list_item.sublist}}
{{/my_list}}
'''

context = {'my_list' : [
        { 'my_list_item' : 
            {'name': 'list first item',
             'sublist' : [
                 {'name': 'sublist first item'},
                 {'name': 'sublist second item'}
                 ]
            }
        },
        { 'my_list_item' : 
            {'name': 'list second item',
             'sublist' : [
                 {'name': 'sublist first item'},
                 {'name': 'sublist second item'}
              ]
            }
        }
    ]}

Thanks a lot!

question from:https://stackoverflow.com/questions/65923875/mustache-pystache-named-context-in-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...