Commit ea1100d4 by Calen Pennington

Add examples of render and render_childe

parent d521a21a
......@@ -14,7 +14,7 @@ class VerticalModule(XModule):
@register_view('student_view')
def get_html(self):
return render_template('vert_module.html', {
'items': [child.render() for child in self.children]
'items': [self.render_child(child) for child in self.children]
})
def get_progress(self):
......
......@@ -186,10 +186,35 @@ class XModule(Plugin, HTMLSnippet):
self.user_preferences = user_preferences
self.student_state = student_state
def render(self, view_name):
@staticmethod
def render(module, view_name):
"""
Render the specified view from the supplied module
view_name: The string name of the view to render
module: The XModule to render
"""
# Make children use the appropriate render context
try:
setattr(module, 'view_name', view_name)
return module.find_view(view_name)()
finally:
delattr(module, 'view_name')
def render_child(self, child, view_name=None):
"""
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
"""
if view_name is None:
view_name = getattr(self, 'view_name')
return XModule.render(child, view_name)
def find_view(self, view_name):
for method_name, method_fn in inspect.getmembers(self, lambda m: inspect.ismethod(m)):
if getattr(method_fn, 'view_name', None) is not None:
return method_fn()
return method_fn
@property
def children(self):
......
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