python - In Jinja2 whats the easiest way to set all the keys to be the values of a dictionary? -


I have a dashboard that gives the name space to the context of each dashboard item There is a quick way I have a key in a template Can you set all values ​​of a dictionary?

I want to reuse templates and not always specify the name of my variable.

My references can be simplified to look at:

  {"": "business": [], "new promotion": [] }, "Message": {"one_to_one": [], "announcements": []}  

With a statement, I set up to include all business items locally Want to Currently I have to set each variable individually:

  {% to%} {% set businesses = business ["business"]%} {set new_promotions = business [" New_promotions "]%} {%" businesses.html "%} {% endwith%}  

I tried:

  {% of With%} {%} key, my_dict%} {% set key = value%} {% endfor%} {% include "businesses.html"%} {% endwith%}  

But they are only scope for the for loop, not including ...

Short story short: you are voluntary in context A variable can not set {% set key = value%} is just setting in determining the value of the variable named key .

The reason for this is that the Gina 2 compilation is done in the Python Code (if you want to see the code generated by your template, then download the script and give it the filename of your template.) To make processing faster, it creates local variables for each variable you use in the template (seeing this only for the first time, looking at the variable) Unlike Engo, which uses the reference for all variables lookup.

To properly generate this code, it should be able to track local or global variables at any time, so it knows when viewing in context and determining the random variable It has to be stopped from working, which is why the reference function s is not allowed to modify the reference, just look at it.

What I will do, however, instead of having a business-display code as a template included, this is another macro in another template. For example, businesses.html :

  {% macro show_businesses (businesses, new promotions)%} {# that you are displaying ... # } {% Endmacro%}  

and then in your main template:

  {% import_businesses%} {% show_businesses (% ** Business)%}  

Or, better yet, separate them into two different macros - for a business, and a very cool template for you to get a new promotion See the trick, and of course, the genie 2 document Please check.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -