Commit 5f739291 by Xavier Antoviaque

Use Django templates for rendering, rename to `render_template`

parent 89639b4b
...@@ -7,6 +7,8 @@ import logging ...@@ -7,6 +7,8 @@ import logging
import os import os
import pkg_resources import pkg_resources
from django.template import Context, Template
from xblock.fragment import Fragment from xblock.fragment import Fragment
...@@ -25,19 +27,20 @@ def load_resource(resource_path): ...@@ -25,19 +27,20 @@ def load_resource(resource_path):
resource_content = pkg_resources.resource_string(__name__, resource_path) resource_content = pkg_resources.resource_string(__name__, resource_path)
return unicode(resource_content) return unicode(resource_content)
def eval_template(template_path, context={}): def render_template(template_path, context={}):
""" """
Evaluate a template by resource path, applying the provided context Evaluate a template by resource path, applying the provided context
""" """
template = load_resource(template_path) template_str = load_resource(template_path)
return template.format(**context) template = Template(template_str)
return template.render(Context(context))
def create_fragment(template_path, context={}): def create_fragment(template_path, context={}):
""" """
Returns a new fragment, with its HTML content initialized to the evaluated template, Returns a new fragment, with its HTML content initialized to the evaluated template,
with the provided context with the provided context
""" """
html = eval_template(template_path, context=context) html = render_template(template_path, context=context)
return Fragment(html) return Fragment(html)
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