Changing dictionary attributes in jinja2

Suppose I pass a dictionary to my jinja2 template.
In the view, I have something like

d = {} #set other template stuff into d get_params['cri'] = 'time' get_params['order'] = 'asc' d['get_params'] = get_params return d 

In the template, I need to change the value of get_params keys. Logical thing

 {% set get_params.cri='src' %} 

with an error

 TemplateSyntaxError: expected token '=', got '.' 

My question is how to change the values ​​passed to the dictionary in jinja2

(This question is asked here , but I find the answer confusing, and it only answers merging)

EDIT answer:

Jinja2 provides the extension 'do'. To add this extension to the pyramid, follow these steps in the __init__.py file

 #This line is alreadythere config.include('pyramid_jinja2') #Add this line config.add_jinja2_extension('jinja2.ext.do') 

In the template

 {% do get_params.update({'cri':'src'}) %} 
+8
python dictionary pyramid jinja2
source share
1 answer

The idea is that you cannot perform assignments in jinja2. However, what you can do (as suggested in another post that you linked) is to call the do block and perform the update operation (updating is the method of any dict in python; http://docs.python.org/library/ stdtypes.html # dict.update ).

+4
source share

All Articles