search

Home  >  Q&A  >  body text

python - How to define nested variables in flask jinjia2

In jinjia, as shown below, I defined a variable number

{% set number = unit %}

file_dict is a dict type variable. I want to reference the key in file_dict so as to find the value in file_dict where the key is {{number}}

{{file_dict{{number}}}}

It seems that it is illegal to define it like the above, and it cannot be passed.
In addition, macro definition cannot be used either.

{% macro get_value(number) %}
             {{ pdf_dict{{number}}}}
{% endmacro %}

Is there any way to define nested variables

迷茫迷茫2714 days ago1252

reply all(1)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-22 11:53:50

    I solved it myself. I was reminded that jinjia2 variables have __getitem__ syntax. Just call get directly instead of accessing the attribute. Because after that, instead of calling the variable, I called the string. Wrapping it directly with double brackets would not work. Grammarly

    reply
    0
  • Cancelreply