Search code examples
pythonmustache

Can pystache tell me if a pattern is not defined?


I have the following script:

import pystache
d = {
    'MSG' : 'bye'
}
print pystache.render('I say {{MSG}} {{THIS_IS_UNDEFINED}}', d)

Which prints:

I say bye 

But I actually want pystache to raise an exception because there is an undefined pattern. In my real code, things are more complicated, so getting hints about what patterns are undefined would be very valuable.

Is it possible to configure pystache for that?


Solution

  • Use a Renderer with missing_tags="strict":

    >>> import pystache
    >>> pystache.Renderer(missing_tags="strict").render("I say {{MSG}} {{THIS_IS_UNDEFINED}}", d)
    KeyNotFoundError: Key u'THIS_IS_UNDEFINED' not found: first part
    

    You could define your own function strictrender:

    def strictrender(s, d):
        renderer = pystache.Renderer(missing_tags='strict')
        return renderer.render(s, d)