Allow TemplateHook to be selective based on URL name
Review Request #1629 — Created May 31, 2010 and submitted
Information | |
---|---|
mike_conley | |
Djblets | |
extensions | |
Reviewers | |
djblets, reviewboard | |
TemplateHook currently allows extensions to insert templates into our Django app. So, for example, we have a hook called "base-scripts-post" which inserts templates on the base template. Now, while creating a TemplateHook, we can supply an optional apply_to list parameter which allows us to specify which view functions should really render the hook. For example: TemplateHook(self, "base-scripts-post", "rbstats/rbstats_dlgs.html", ["dashboard"]) Now rbstats_dlgs.html will only be rendered on the dashboard view function. I'm not entirely sure if my way of finding "func_name" is the way to go. Open to suggestions.
Manual.
-
-
djblets/extensions/templatetags/djblets_extensions.py (Diff revision 1) Hmm yeah, I'm not entirely sure what I think of using the function name. I'd key off the URL name actually (as defined in the urls.py files). It's possible for a single function to handle multiple URLs too (in the case of iPhone vs. main UI, for example), and we don't want to have collisions there.
Change Summary:
This patch relies on http://reviews.reviewboard.org/r/1674/ Ok, found a solution to this problem. I've written a tiny Middleware to stash kwargs into the request META. This, coupled with the URL name, allows us to use "reverse" to determine what the current URL should be. If it *is* the current URL, then we should render the TemplateHook.
Diff: |
Revision 2 (+48 -4) |
---|
-
Awesome! Glad you found something that works here.
-
djblets/extensions/hooks.py (Diff revision 2) Should be on the same indentation level as the starting """.
-
djblets/extensions/hooks.py (Diff revision 2) Is there a reason we need to include this here and not at the top of the file?
-
-
djblets/extensions/middleware.py (Diff revision 2) I'm not sure if we should be stashing it here or elsewhere, but what we do elsewhere is just request._djblets_whatever = value.
Change Summary:
Originally, I caught a NoReverseMatch exception if the URL didn't take kwargs. I forgot to catch the *other* exception, in the event that there's no match even without the kwargs.
Diff: |
Revision 4 (+51 -4) |
---|