Mako's problem in the pyramid

I need to put javascript function in mako template. The first argument to this function is a string, so I write in the * .mako file (dict (field_name = 'geom)):

init_map( '${field_name}' ); 

But when I see my html page, it looks like this:

 init_map( 'geom' ) 

How can I turn off escaping in this case?

Rendering is done as follows:

 from pyramid.renderers import render render('georenderer/map.mako', template_args) 
+6
source share
2 answers

You need to include quotes in your expression, I think. You can use the json module to output valid JavaScript letters:

 dict(field_name=json.dumps('geom')) 

and in your template:

 init_map( ${field_name | n} ); 

Then the quotes are generated by the .dumps() function, and the filter | n | n ensures that they are not shielded; You have already made your values ​​safe JavaScript, you also do not need these HTML files.

An added benefit is that the module will also escape any quotes in your JavaScript values ​​and handle unicode correctly:

 >>> import json >>> print json.dumps(u'Quotes and unicode: " \u00d8') "Quotes and unicode: \" \u00d8" 
+5
source

Try filter n . According to docs , it disables shielding (or any other filtering by default):

 ${field_name | n} 

UPDATE: Sorry, I did not notice the quotation marks around the expression. And now it seems very strange ...

+1
source

Source: https://habr.com/ru/post/923826/


All Articles