New FAMILUG

The PyMiers

Wednesday 19 October 2016

Truy cập đến key của một dict trong Jinja

Thay vì phải viết
adict['mykey']
ta có thể viết
adict.mykey

Trích tài liệu của Jinja:
You can use a dot (.) to access attributes of a variable in addition to the standard Python __getitem__ “subscript” syntax ([]).
The following lines do the same thing:
{{ foo.bar }}
{{ foo['bar'] }}
Chú ý rằng 2 syntax này sẽ cho kết quả khác nhau nếu object có attribute trùng tên với key trong dict mà nó chứa.
This is important if an object has an item and attribute with the same name. Additionally, the attr() filter only looks up attributes.
Hết
HVN at FAMILUG dot ORG

No comments:

Post a Comment