Mako escaping issue within Pyramid

2024/9/8 10:20:26

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

init_map('${field_name}'
);

But when I see my html page it loks like:

init_map('geom'
)

How I can disable escaping in this case?

Rendering performs the following way:

from pyramid.renderers import render
render('georenderer/map.mako', template_args)
Answer

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

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

and in your template:

init_map(${field_name | n}
);

The quotes are then generated by the .dumps() function, and the | n filter ensures they are not escaped; you've already made your values JavaScript safe, you don't need them HTML-safe either.

The added advantage is that the module will escape any quotes in your JavaScript values as well, and handle unicode properly:

>>> import json
>>> print json.dumps(u'Quotes and unicode: " \u00d8')
"Quotes and unicode: \" \u00d8"
https://en.xdnf.cn/q/72804.html

Related Q&A

All arguments should have the same length plotly

I try to do a bar graph using plotly.express but I find this problemAll arguments should have the same length. The length of argument y is 51, whereas the length of previously-processed arguments [x] …

Python curves intersection with fsolve() and function arguments using numpy

I am trying to use fsolve as quoted here : http://glowingpython.blogspot.gr/2011/05/hot-to-find-intersection-of-two.html,On order to find the intersection between two curves. Both curves basically are …

What is the Python freeze process?

The Python Doc states:Frozen modules are modules written in Python whose compiled byte-codeobject is incorporated into a custom-built Python interpreter byPython’s freeze utility. See Tools/freeze/ fo…

Is there a way to get the top k values per row of a numpy array (Python)?

Given a numpy array of the form below:x = [[4.,3.,2.,1.,8.],[1.2,3.1,0.,9.2,5.5],[0.2,7.0,4.4,0.2,1.3]]is there a way to retain the top-3 values in each row and set others to zero in python (without an…

Python with tcpdump in a subprocess: how to close subprocess properly?

I have a Python script to capture network traffic with tcpdump in a subprocess:p = subprocess.Popen([tcpdump, -I, -i, en1,-w, cap.pcap], stdout=subprocess.PIPE) time.sleep(10) p.kill()When this script …

How to install GDB with Python support on Windows 7

I need to debug cython code. Official documentation says, I need to install "gdb 7.2 or higher, built with Python support". Unfortunately I didnt find any step-by-step guide how to install it…

Pip3 is unable to install requirements.txt during docker build

I am using docker tutorial (https://docs.docker.com/language/python/build-images/) to build a simple python app. Using freeze command I made requirements.txt file which consists a lot of packages. When…

__del__ at program end

Suppose there is a program with a couple of objects living in it at runtime.Is the __del__ method of each object called when the programs ends?If yes I could for example do something like this:class C…

PySpark groupby and max value selection

I have a PySpark dataframe likename city datesatya Mumbai 13/10/2016satya Pune 02/11/2016satya Mumbai 22/11/2016satya Pune 29/11/2016satya Delhi 30/11/2016panda Delhi 29/11/2016…

Nesting descriptors/decorators in python

Im having a hard time understanding what happens when I try to nest descriptors/decorators. Im using python 2.7.For example, lets take the following simplified versions of property and classmethod:clas…