Issues

Issue #20 resolved

Syntax error with ${{'foo':'bar'}}

Anonymous created an issue

This works fine:

<li>${dict(method="get")}</li>

This crashes:

<li>$'method':'get'</li>

mako.exceptions.SyntaxException: (SyntaxError) unexpected EOF while parsing (line 1) ("{'method':'get'") in file [...]

Comments (12)

  1. Mike Bayer repo owner
    • changed status to open
    • marked as
    • changed assignee to zzzeek

    yeah unfortunately there will always be some issues like this one, it should be pretty obvious why this one is occurring (you know, its looking for a bracket). there is some logic to handle things like this which is already accounting for strings like ${"}") but thats because its counting quotes...i can look into it further but unless i rewrote the entire python parser there will always be things like this which can slip through (also, a typical MVC setup wouldnt really be declaring dictionaries inside of templates/expressions anyway, though thats not a 100% excuse.....)

    id like to see how other template languages handle phrases like that one.

  2. Anonymous

    I've simplfied the example for the demonstration, but following this example, this is not an excuse, not even 1%. :)

    From a Pylons point of view, I'd be tempted to do the following:

    ${h.link_to_remote("Home", options={"url":h.url_for("home"), "method":"get"})}

    How about extracting what's contained inside ${} as a string and do an eval() on the string ?

    extracted = """h.link_to_remote("Home", options={"url":h.url_for("home"), "method":"get"})""" print eval(extracted)

    <a href="#" onclick="new Ajax.Request('home', {asynchronous:true, evalScripts:true, method:'get'}); return false;">Home</a>

    Just an idea...

  3. P C

    Rather than fixing every such bug, may be a good idea to let user to increment number of braces if he needs. So that examples above writen as:

    $h.link_to_remote("Home", options={"url":h.url_for("home"), "method":"get"}) ${'method':'get'}

    1. So this would be also written as: $" some } text "
    2. And maybe sometimes you will need: $ {'a': b, c: { 'e': 'f' }}
    3. but this is better writen as (with space between braces "}"): ${'a': b, c: { 'e': 'f' } }

    I consider this is better than escaping braces someway:

    ${ h.link_to_remote("Home", options=\{"url":h.url_for("home"), "method":"get"\}) }

    1. or maybe ${ h.link_to_remote("Home", options="url":h.url_for("home"), "method":"get") }
  4. guest

    Attached patch exploits `codeop` module to overcome this problem. This module is one that is used for interactive python sessions.

  5. Mike Bayer repo owner

    unfortunately, many tests fail with the latest patch. If the patch can be repaired such that all tests pass, we can put it in. Mako is due for a release so this one might have to wait for another go-around....

  6. guest

    Tavis has the following suggestion from Cheetah. -MO

    "I maintain a stack of enclosures (,[,{ to balance things out. See getExpressionParts at line 1052 in Parser.py."

  7. Mike Bayer repo owner

    yeah, i think an expansion of the current "quote counting" idea to just be a stack of enclosures is the most practical approach here. I had hopes for the "python compiler" approaches previously patched here but this would be more straightforward.

  8. Mike Bayer repo owner

    k, thats hopeful. i'm at energy level zero for mako these days so if someone wants to adapt that into a patch, that's a good path towards a fix.

  9. Mike Bayer repo owner

    wow, that is really impressive ! as you can see, this bug has existed since day one and...ah someone else took a crack five years ago. I'll try running this across a huge pile of templates here and see what I get.

  10. Log in to comment