Question

I am using mako template with python and trying to generate a text file by using a list from python script. A part of code is shown below which is causing problem.

% for COMPNAME in tpdob.scalar_modi:
    ${COMPNAME[0]} ${COMPNAME[1]} ${COMPNAME[2]} ${COMPNAME[3]}
% endfor

I am using this code and the output is horrible.
Here, I have many lists in scalar_modi and I am printing them one by one (each list has 4 values). But the problem is, there are so many newlines that gets printed making the output text file look ugly. I searched on net, but unable to find the required solution. Can you please help me out with this?

Comments on output:

With ${COMPNAME[0]} ${COMPNAME[1]} ${COMPNAME[2]} ${COMPNAME[3]} , I am getting:
1 42 gzb dli

14 23 tpty sre

32 55 puri ald


When using ${COMPNAME[0]} ${COMPNAME[1]} ${COMPNAME[2]} ${COMPNAME[3]} \, gives : 11 42 gzb dli14 23 tpty sre32 55 puri ald


And I want it like:
11 42 gzb dli
14 23 tpty sre
32 55 puri ald


And when using if-else conditions, I am getting even more than 2 newlines. Can anyone please help me with this. I am unable to find the solution in its documentation also :(

Was it helpful?

Solution

I had the same problem, looks like a bug in Mako Templates for Windows. Providing the template as a string instead of a file reference (as shown below) solved the problem.

with open('code.template') as f:
    templateStr = f.read()
template = mako.template.Template(templateStr)

OTHER TIPS

Old topic but I just found out a better approach using the preprocessor kwarg:

tpl_lookup = TemplateLookup(directories=['./templates'], output_encoding='utf-8', module_directory='./tmp', preprocessor=[lambda x: x.replace("\r\n", "\n")])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top