Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lucas-C opened this issue Jul 18, 2016 · 1 comment
Open

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

Lucas-C opened this issue Jul 18, 2016 · 1 comment

Comments

@Lucas-C
Copy link

Lucas-C commented Jul 18, 2016

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
Copy link
Author

Lucas-C commented Jul 18, 2016

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"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant