diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2013-05-14 16:09:55 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2013-05-14 16:09:55 -0500 |
commit | b312d2cd83dbbfb32adc30c5eb3a9a4cc6ae9295 (patch) | |
tree | ad435d084702d8e5ae1d3edad3704e34dab454ab /docs/source/pluginwriter | |
parent | 5046ca24337cadff9b95782101f0a8c4d456e15e (diff) | |
download | mediagoblin-b312d2cd83dbbfb32adc30c5eb3a9a4cc6ae9295.tar.lz mediagoblin-b312d2cd83dbbfb32adc30c5eb3a9a4cc6ae9295.tar.xz mediagoblin-b312d2cd83dbbfb32adc30c5eb3a9a4cc6ae9295.zip |
Added documentation on view specific hooks
This commit sponsored by Matthew Woodward. Thank you!
Diffstat (limited to 'docs/source/pluginwriter')
-rw-r--r-- | docs/source/pluginwriter/api.rst | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/docs/source/pluginwriter/api.rst b/docs/source/pluginwriter/api.rst index df933511..0d5c82d8 100644 --- a/docs/source/pluginwriter/api.rst +++ b/docs/source/pluginwriter/api.rst @@ -48,3 +48,65 @@ example might look like:: This means that when people enable your plugin in their config you'll be able to provide defaults as well as type validation. + +Context Hooks +------------- + +View specific hooks ++++++++++++++++++++ + +You can hook up to almost any template called by any specific view +fairly easily. As long as the view directly or indirectly uses the +method ``render_to_response`` you can access the context via a hook +that has a key in the format of the tuple:: + + (view_symbolic_name, view_template_path) + +Where the "view symbolic name" is the same parameter used in +``request.urlgen()`` to look up the test. So say we're wanting to add +something to the context of the user's homepage. We look in +mediagoblin/user_pages/routing.py and see:: + + add_route('mediagoblin.user_pages.user_home', + '/u/<string:user>/', + 'mediagoblin.user_pages.views:user_home') + +Aha! That means that the name is ``mediagoblin.user_pages.user_home``. +Okay, so then we look at the view at the +``mediagoblin.user_pages.views:user_home`` method:: + + @uses_pagination + def user_home(request, page): + # [...] whole bunch of stuff here + return render_to_response( + request, + 'mediagoblin/user_pages/user.html', + {'user': user, + 'user_gallery_url': user_gallery_url, + 'media_entries': media_entries, + 'pagination': pagination}) + +Nice! So the template appears to be +``mediagoblin/user_pages/user.html``. Cool, that means that the key +is:: + + ("mediagoblin.user_pages.views:user_home", + "mediagoblin/user_pages/user.html") + +The context hook uses ``hook_transform()`` so that means that if we're +hooking into it, our hook will both accept one argument, ``context``, +and should return that modified object, like so:: + + def add_to_user_home_context(context): + context['foo'] = 'bar' + return context + + hooks = { + ("mediagoblin.user_pages.views:user_home", + "mediagoblin/user_pages/user.html"): add_to_user_home_context} + + +Global context hook ++++++++++++++++++++ + + |