Modulo operator inside Mako template

41 views
Skip to first unread message

Graeme Gemmill

unread,
Jan 15, 2019, 12:02:08 PM1/15/19
to Mako Templates for Python
I need to place data alternately left justified and right justified in a template. My code is:
 <!-- begin:sollist -->
      % for row in sollist:
              <tr>
              nc = 2
              % for cell in row:
                  % if nc % 2 == 0:
                    <td class='aleft'>${cell}</td>
                  % else:
                    <td class='aright'>${cell}</td>
                  % endif
              % endfor
              nc += 1
          </tr>
      % endfor
      <!-- end:sollist -->
This is as example of "row":
['Sun Rise', '2019-01-14 08:06:19\n']
My error traceback is:
Traceback (most recent call last):
 File "weather.py", line 771, in <module>
   main()
 File "weather.py", line 754, in main
   tmpl.render_context(ctx)
 File "/usr/lib/python3.5/site-packages/mako/template.py", line 485, in render_context
   **kwargs)
 File "/usr/lib/python3.5/site-packages/mako/runtime.py", line 873, in _render_context
   _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
 File "/usr/lib/python3.5/site-packages/mako/runtime.py", line 899, in _exec_template
   callable_(context, *args, **kwargs)
 File "/tmp/mako_modules/weather.html.py", line 164, in render_body
   if nc % 2 == 0:
TypeError: unsupported operand type(s) for %: 'Undefined' and 'int'

Is this a problem with the modulo operator?
Help please
Graeme

Walter Jaisli

unread,
Jan 15, 2019, 1:12:30 PM1/15/19
to mako-d...@googlegroups.com
try
<%
  nc = 2
%>


and
<%
  nc +=1
%>


Walter


--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mako-discuss...@googlegroups.com.
To post to this group, send email to mako-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/mako-discuss.
For more options, visit https://groups.google.com/d/optout.

Graeme Gemmill

unread,
Jan 16, 2019, 5:42:57 AM1/16/19
to Mako Templates for Python
Many thanks, Walter, works a treat. Is this documented anywhere in the Mako docs?
Graeme

Walter Jaisli

unread,
Jan 16, 2019, 8:47:55 AM1/16/19
to mako-d...@googlegroups.com
Hallo Graeme,
yes this ist standard Mako:
Your template is HTML.  Statements like x = y   are Python.
Python Statements must be in between <%   and %>

Walter 

Diane Wellman

unread,
Jan 16, 2019, 3:18:13 PM1/16/19
to Mako Templates for Python
Hello Graeme,

I see that the more specific modulo mako question was answered. I wanted to add two things. 

1) You can also enumerate to handle looping, which makes the code a bit cleaner. 

% for i, cell in enumerate(row):
<td  class="${'aleft' if i % 2 else 'aright'}">${cell}</td>
% endfor



2) If you are making HTML to be displayed in a browser, you could also use css to handle the cell alignment logic.

<style>
  td:nth-child(even) { text-align: right; }  
</style>
 
% for cell in row:
<td>${cell}</td>
% endfor

CSS psudo-classes can also be used to target a specific column, every n columns, zebra striped rows, etc... 
Here is quick demo of the common patterns. https://codepen.io/stringtheory/pen/qLwYdm

Diane

Mike Bayer

unread,
Jan 16, 2019, 4:04:58 PM1/16/19
to mako-d...@googlegroups.com
On Wed, Jan 16, 2019 at 3:18 PM Diane Wellman <di...@beautylish.com> wrote:
>
> Hello Graeme,
>
> I see that the more specific modulo mako question was answered. I wanted to add two things.
>
> 1) You can also enumerate to handle looping, which makes the code a bit cleaner.
>
> % for i, cell in enumerate(row):
> <td class="${'aleft' if i % 2 else 'aright'}">${cell}</td>
> % endfor
>
> https://docs.makotemplates.org/en/latest/runtime.html#cycling

that link illustrates the more succinct way which is to use the loop context:

<ul>
% for item in ('spam', 'ham', 'eggs'):
<li class="${loop.cycle('even', 'odd')}">${item}</li>
% endfor
</ul>



>
>
> 2) If you are making HTML to be displayed in a browser, you could also use css to handle the cell alignment logic.
>
> <style>
> td:nth-child(even) { text-align: right; }
> </style>
>
> % for cell in row:
> <td>${cell}</td>
> % endfor
>
> CSS psudo-classes can also be used to target a specific column, every n columns, zebra striped rows, etc...
> Here is quick demo of the common patterns. https://codepen.io/stringtheory/pen/qLwYdm
>
> Diane
>
Reply all
Reply to author
Forward
0 new messages