Closed martinburchell closed 2 years ago
Thanks! In this case, in table.mako
, col_index
is guaranteed to be an int. So if that's the problem, we could use ${str(col_index) | n }
(string conversion in Python, disable default filtering) as the likely fastest option. But I'm a bit puzzled -- when Mako renders an integer in a template, what else can it do except convert it to a string (since everything is being converted to a string)? Is it col_index
that's causing the problem, or table content further down (via val
), or both?
I think we can just use ${col_index}
.
There's a problem with ${ val | n }
as well. An integer value ends up in one of the cells.
OK. So ${ str(val) | n }
for the unescaped version?
Yes or ${ val | n,h }
and I'd like to investigate if this is a bug in Mako.
I meant ${ val | n,str }
above and in the original description
>>> from mako.template import Template
>>> t = Template("${ 0 | n}", default_filters=["h"])
>>> t.render()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/template.py", line 473, in render
return runtime._render(self, self.callable_, args, data)
File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/runtime.py", line 885, in _render
return context._pop_buffer().getvalue()
File "/home/martinb/.virtualenvs/camcops/lib/python3.6/site-packages/mako/util.py", line 169, in getvalue
return self.delim.join(self.data)
TypeError: sequence item 0: expected str instance, int found
>>> t = Template("${ 0 | n,str}", default_filters=["h"])
>>> t.render()
'0'
The problem seems to be that in disabling the HTML escaping (
h
filter) withn
, we are also disabling conversion of int to str.https://github.com/RudolfCardinal/camcops/blob/master/server/camcops_server/templates/snippets/table.mako#L42 https://github.com/RudolfCardinal/camcops/blob/master/server/camcops_server/templates/snippets/table.mako#L46
We don't need to disable filtering of
${col_index}
but my understanding from https://docs.makotemplates.org/en/latest/filtering.html is thatn
should disable the default filters. In our case that the default filters are just["h"]
https://github.com/RudolfCardinal/camcops/blob/master/server/camcops_server/cc_modules/cc_pyramid.py#L329. However, If I change| n
to| n,h
(just disableh
) in table.mako the problem goes away.