Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Created January 3, 2013 17:24
Show Gist options
  • Save al-the-x/4445122 to your computer and use it in GitHub Desktop.
Save al-the-x/4445122 to your computer and use it in GitHub Desktop.
While using PyHAML to build an app with AngularJS, I noticed that some of my attributes were being "sanitized" when I use a `def` or `block` tag with HAML syntax (e.g. `%%self:some_def` below). The PyHAML preprocessor expands the attribute name on the tag to `ng-app`, then Mako barfs on the `ng-` prefix of the attribute. Woes!
- attrs={ "ngApp": "something" }
%p(**attrs)
This element should have an attribute <code>ng-app="something"</code>. |
It works just fine.
%%(def(name="some_def(**attrs)")
= attrs
%p(**attrs)
= caller.body() or 'Cannot pass a body to a function... :/'
${some_def(ngApp="something")
I can't pass a body with this invocation, can I...?
%%self:some_def(ngApp="something")
This element should have an attribute <code>ng-app="something"</code>, too. |
But something went wrong.
<%! from haml import runtime as __HAML %>
<%
attrs={ "ngApp": "something" }
%>
<p<% __M_writer(__HAML.attribute_str(**attrs)) %>>
This element should have an attribute <code>ng-app="something"</code>.
It works just fine.
</p>
<%def name="some_def(**attrs)">
${attrs}
<p<% __M_writer(__HAML.attribute_str(**attrs)) %>>
${caller and caller.body()}
</p>
</%def>
${some_def(ngApp="something")}
I can't pass a body with this invocation, can I...?
<%self:some_def ng-app="something">
This element should have an attribute <code>ng-app="something"</code>, too.
But something went wrong.
</%self:some-def>
Traceback (most recent call last):
. . .
mako.exceptions.SyntaxException: Expected %> at line: 18 char: 53
@al-the-x
Copy link
Author

al-the-x commented Jan 3, 2013

As a workaround, I can write the full Mako tag instead, but it's kind of a pain. Maybe PyHAML shouldn't process attributes of Mako tags the same way it does regular HTML tags...? I'd say that Mako probably shouldn't barf on hyphenated tag attributes, too, but maybe I'm the only one using them this way...

@mikeboers
Copy link

Tweaked slightly so it will actually run:

- attrs={ "ngApp": "something" }
%p(**attrs)
  This element should have an attribute <code>ng-app="something"</code>. |
  It works just fine.

%%def(name="some_def(**attrs)")
  = attrs
  %p(**attrs)
    = caller.body() if caller else 'Cannot pass a body to a function... :/'

${some_def(ngApp="something")}
  I can't pass a body with this invocation, can I...?

%%self:some_def(ngApp='something')
  The ngApp is not converted for the Mako tag, but it will be in the %gt;p%lt;.

I agree that attributes of Mako tags should not be expanded. I am patching it now...

@mikeboers
Copy link

@mikeboers
Copy link

It has been released to PyPI as v0.1.9.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment