Created
January 3, 2013 17:24
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%! 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
. . . | |
mako.exceptions.SyntaxException: Expected %> at line: 18 char: 53 |
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...
Fixed in mikeboers/PyHAML@310d5b1
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
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...