tal nested dictionary syntax

。_饼干妹妹 提交于 2019-12-11 08:30:35

问题


Working with Pyramid, my code looks like this:

class PageData:
    @staticmethod
    def create_data():
        return [
            {   
                'key_1A': 'info1A',
                'key_2A': 'info2A',
                'nested_list_A': [
                    {'nested_key1A': 'nested_val1A'},
                    {'nested_key2A': 'nested_val2A'},
                ],
            },
            {   
                'key_1A': 'info1B',
                'key_2A': 'info2B',
                'nested_list_B': [
                    {'nested_key1B': 'nested_val1B'},
                    {'nested_key2A': 'nested_val2A'},
                ],
            },
            ]

And my html page code looks like this:

<span tal:condition="nested_key1A">     Open     </span>
<span tal:condition="not nested_key1A"> Closed   </span>

What is the proper syntax to reference nested_key? for a tal:condition statement?


回答1:


In trying to figure this out I found my answer...

tal:repeat Syntax: tal:repeat="name expression"

Description: Evaluates "expression", and if it is a sequence, repeats this tag and all children once for each item in the sequence. The "name" will be set to the value of the item in the current iteration, and is also the name of the repeat variable. The repeat variable is accessible using the TAL path: repeat/name and has the following properties:

https://www.owlfish.com/software/simpleTAL/tal-guide.html

<div tal:repeat="a nest_list_A">
<div tal:repeat="b a.nest_list_A">
<span tal:condition="b.nested_key1A">

a becomes the assignment for nest_list_A eg b becomes the assignment for a.nested_list_A which will then access they key

if there is a value for the key, then tal:condition will continue as normal, otherwise it will skip while rendering.



来源:https://stackoverflow.com/questions/45319983/tal-nested-dictionary-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!