pythongenshi

Genshi: curly brace inside complex expression causes ParseError


I have the following Genshi HTML template snippet that is being used to generate HTML.

<div>
    ${'Hello {name}'.format(name='foo')}
</div>

Genshi throws a fit over this due to the curly braces in the string.

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\genshi\input.py", line 161, in _generate
    self.expat.Parse(data, False)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 137, column 34

Is there a way to escape the curly braces? Is there something better I should be using than the ${...code...} syntax?


Solution

  • Unless you're doing something more complex (code/function), you could do:

    <div>
        Hello ${name}  // or just Hello $name
    </div>
    

    For something complex, use the code block syntax:

    <div>
      <?python
          def greeting(name):
              return 'Hello {name}'.format(name=name) ?>
      ${greeting('foo')}
    </div>