Commit 66e47f74 by Ned Batchelder

Add a context for rendering.

parent 8d790784
...@@ -88,7 +88,7 @@ class Plugin(object): ...@@ -88,7 +88,7 @@ class Plugin(object):
def register_view(view_name): def register_view(view_name):
def wrapper(fn): def wrapper(fn):
setattr(fn, 'view_name', view_name) fn.view_name = view_name
return fn return fn
return wrapper return wrapper
...@@ -133,21 +133,26 @@ class XModule(Plugin): ...@@ -133,21 +133,26 @@ class XModule(Plugin):
self._view_name = None self._view_name = None
@staticmethod @staticmethod
def render(module, view_name): def render(module, view_name, context):
""" """
Render the specified view from the supplied module Render the specified view from the supplied module
view_name: The string name of the view to render
module: The XModule to render module: The XModule to render
view_name: The string name of the view to render
context: Data parent XModules make available to their children
during rendering.
""" """
# Make children use the appropriate render context # Make children use the appropriate render context
try: try:
module._view_name = view_name module._view_name = view_name
return module.find_view(view_name)() return module.find_view(view_name)(context)
finally: finally:
module._view_name = None module._view_name = None
def render_child(self, child, view_name=None): def render_child(self, child, view_name=None, context=None):
""" """
Render a view on a child module. If view_name isn't supplied, Render a view on a child module. If view_name isn't supplied,
render the same view on the child that is currently being rendered on the parent render the same view on the child that is currently being rendered on the parent
...@@ -155,11 +160,11 @@ class XModule(Plugin): ...@@ -155,11 +160,11 @@ class XModule(Plugin):
if view_name is None: if view_name is None:
view_name = self._view_name view_name = self._view_name
return XModule.render(child, view_name) return XModule.render(child, view_name, context or {})
def find_view(self, view_name): def find_view(self, view_name):
for method_name, method_fn in inspect.getmembers(self, inspect.ismethod): for method_name, method_fn in inspect.getmembers(self, inspect.ismethod):
if getattr(method_fn, 'view_name', None) is not None: if getattr(method_fn, 'view_name', None) == view_name:
return method_fn return method_fn
raise MissingXModuleView(self.__class__, view_name) raise MissingXModuleView(self.__class__, view_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment