def render(template, context=None, name=None, **kwargs): """ Return the given template string rendered using the given context. """ renderer = Renderer() parsed_template = parse(template, name=name) return renderer.render(parsed_template, context, **kwargs)
def test_make_resolve_partial__parsed(self): """ Test _make_resolve_partial__parsed(): that we can take ParsedTemplates as partials """ partials = {"partial": parse(u"Hello, {{person}}")} renderer = Renderer(partials=partials) actual = renderer.render(u"{{>partial}}", {"person": "foo"}) self.assertString(actual, u"Hello, foo")
def render(self, template, context_stack, delimiters=None, name=None): """Render a template string, and return as str. Arguments: template: a template string of type str (but not a proper subclass of str). context_stack: a ContextStack instance. """ parsed_template = parse(template, delimiters, name) return parsed_template.render(self, context_stack)
def render(self, template, context_stack, delimiters=None): """ Render a unicode template string, and return as unicode. Arguments: template: a template string of type unicode (but not a proper subclass of unicode). context_stack: a ContextStack instance. """ parsed_template = parse(template, delimiters) return parsed_template.render(self, context_stack)
def render_name(self, template_name, *context, **kwargs): try: parsed_template = None cache_key = "%s_template_%s" % (options.site_name, template_name) parsed_template = self.memcache_get(cache_key) if not parsed_template: loader = self._make_loader() template = loader.load_name(template_name) template = self._to_unicode_hard(template) parsed_template = parse(template, None) self.memcache_set(cache_key, parsed_template) stack = ContextStack.create(*context, **kwargs) self._context = stack engine = self._make_render_engine() return parsed_template.render(engine, stack) except Exception as e: logging.exception(e) return e.message
def _load_template(path_to_template): """ Loads the template from the given location on the file system parsed templates are cached for future access Does not cache compiled templates if django is in DEBUG mode """ webapp_root = settings.WEBAPP_ROOT """ if not settings.DEBUG: webapp_root = os.path.join(webapp_root, 'build/') else: webapp_root = os.path.join(webapp_root, 'web/') """ normpath = os.path.normpath(path_to_template) abspath = os.path.join(webapp_root, normpath) if settings.DEBUG or path_to_template not in _PRECOMPILED_TEMPLATES: with open(abspath) as f: template_str = f.read() _PRECOMPILED_TEMPLATES[path_to_template] = parse(template_str, delimiters=DELIMITERS) return _PRECOMPILED_TEMPLATES[path_to_template]