Added documentation on view specific hooks
This commit sponsored by Matthew Woodward. Thank you!
This commit is contained in:
parent
5046ca2433
commit
b312d2cd83
@ -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
|
||||
+++++++++++++++++++
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user