Search code examples
pythontemplatesmako

The ${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}} syntax doesn't work in a Mako template


In a Mako template, I need to do something like:

${'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'}}

When A do that, I get this error:

SyntaxException: (SyntaxError) unexpected EOF while parsing
(, line 1) ("'foo %(a)s bar %(b)s' % {'a': '1', 'b': '2'") in file…

How do I fix this issue?

I need to use this syntax in translated text:

$(_(u'foo bar %(a)s ... %(b)s) % { ... })

Solution

  • A work-around is to pass the dict object in a different way. For example:

    from mako.template import Template
    
    print Template("${'foo %(a)s bar %(b)s' % data}").render(data=dict(a='Alpha',b='Beta'))