defunkt / pystache

Mustache in Python
http://mustache.github.com/
MIT License
1.31k stars 308 forks source link

EAFP in pystache.context:_get_value to support defaultdicts #188

Open Lucas-C opened 8 years ago

Lucas-C commented 8 years ago

Hi,

Following the "it's Easier to Ask Forgiveness than Permission" principle, is it possible to modify 2 lines in pystache.context:_get_value ?

    if isinstance(context, dict):
        try:
            return context[key]
        except KeyError:
            pass

This would make it possible to pass a collections.defaultdict to pystache.render, which does not work currently:

pystache.render("Hello {{}} !", defaultdict(lambda: "Anonymous visitor"))
Lucas-C commented 8 years ago

Currently I use this ugly workaround to perform what I want:

class DefaultDictContainsEverything(defaultdict):
    def __contains__(self, item):
        return True

pystache.render("Hello {{}} !", DefaultDictContainsEverything(lambda: "Anonymous visitor"))