dave mankoff
dave mankoff

Reputation: 17769

Using "class" as an option to a Mako Macro

I am writing a Mako extension that assist in rendering forms. I was making excellent progress until I ran into one big stumbling block regarding css classes. I want to write code that looks like this:

<%fp:form method="post" action="" class="css-class"%>
...content
</%fp:form>

The function that fp:form refers to looks simply like:

@supports_caller
def form(context, **kwargs):
     #...

When I run this code, I get the following exception:

SyntaxException: (SyntaxError) invalid syntax (<unknown>, line 1) (u"fp.form(method=u'post',action=u'',class=u'css-class')") at line: 1 char: 52

This only happens when the "class" attribute is specified, presumably because Mako is converting the attributes directly to keyword arguments rather than dictionary unpacking. I tried passing a special "attrs" argument to my function, but there seems to be no good way to pass a dict to Mako:

<%fp:form method="post" action="" attrs="${{'class':'css-class'}}"%>
#SyntaxError: invalid syntax
<%fp:form method="post" action="" attrs="${dict(class='css-class')}"%>
#Won't work - can't pass 'class' directly as a keyword argument!

So how do I get around this? I know I can specify a special attribute, like "css_class" and convert that into class, but that's a bit of a hack and further prevents the library from every producing a form with an attribute with css_class.

Upvotes: 2

Views: 385

Answers (1)

Jochen Ritzel
Jochen Ritzel

Reputation: 107588

There is no way around this because class is a keyword and is always parsed as such. People usually use class_ instead.

Upvotes: 2

Related Questions