raise TemplateAssertionError('It\'s not possible to set and '
'access variables derived from '
'an outer scope! (affects: %s' %
- vars, node.lineno, self.filename)
+ vars, node.lineno, self.name)
# remove variables from a closure from the frame's undeclared
# identifiers.
if block.name in self.blocks:
raise TemplateAssertionError('block %r defined twice' %
block.name, block.lineno,
- self.filename)
+ self.name)
self.blocks[block.name] = block
# generate the root render function.
if not frame.toplevel:
raise TemplateAssertionError('cannot use extend from a non '
'top-level scope', node.lineno,
- self.filename)
+ self.name)
# if the number of extends statements in general is zero so
# far, we don't have to add a check if something extended
self.writeline('parent_root = environment.get_template(', node, 1)
self.visit(node.template, frame)
- self.write(', %r).root_render_func' % self.filename)
+ self.write(', %r).root_render_func' % self.name)
# if this extends statement was in the root level we can take
# advantage of that information and simplify the generated code
def compile(self, source, name=None, filename=None, raw=False,
globals=None):
- """Compile a node or source."""
+ """Compile a node or source. The name is the load name of the
+ template after it was joined using `join_path` if necessary,
+ filename is the estimated filename of the template on the file
+ system. If the template came from a database or memory this
+ can be omitted.
+ """
if isinstance(source, basestring):
source = self.parse(source, name)
if self.optimized:
if raw:
return source
if filename is None:
- filename = '<from_string>'
+ filename = '<template>'
elif isinstance(filename, unicode):
filename = filename.encode('utf-8')
return compile(source, filename, 'exec')
self.name = name
-class TemplateSyntaxError(SyntaxError, TemplateError):
+class TemplateSyntaxError(TemplateError):
"""
Raised to tell the user that there is a problem with the template.
"""
- def __init__(self, message, lineno, filename):
- SyntaxError.__init__(self, '%s (line %s)' % (message, lineno))
+ def __init__(self, message, lineno, name):
+ TEmplateError.__init__(self, '%s (line %s)' % (message, lineno))
self.message = message
self.lineno = lineno
- self.filename = filename
+ self.name = name
class TemplateAssertionError(AssertionError, TemplateSyntaxError):
- def __init__(self, message, lineno, filename):
+ def __init__(self, message, lineno, name):
AssertionError.__init__(self, message)
- TemplateSyntaxError.__init__(self, message, lineno, filename)
+ TemplateSyntaxError.__init__(self, message, lineno, name)
class TemplateRuntimeError(TemplateError):
the exported variables for example).
"""
- def __init__(self, environment, globals, filename, blocks, standalone):
+ def __init__(self, environment, globals, name, blocks, standalone):
dict.__init__(self, globals)
self.environment = environment
self.exported = set()
- self.filename = filename
+ self.name = name
self.blocks = dict((k, [v]) for k, v in blocks.iteritems())
# if the template is in standalone mode we don't copy the blocks over.
return '<%s %s of %r>' % (
self.__class__.__name__,
dict.__repr__(self),
- self.filename
+ self.name
)
template = environment.get_template(template)
gen = template.root_render_func(context, standalone=True)
context = gen.next()
- self._filename = template.name
+ self._name = template.name
self._rendered_body = u''.join(gen)
self._context = context.get_exported()
def __repr__(self):
return '<%s %r>' % (
self.__class__.__name__,
- self._filename
+ self._name
)
def escape(obj, attribute=False):
"""HTML escape an object."""
- if hasattr(obj, '__html__'):
+ if obj is None:
+ return u''
+ elif hasattr(obj, '__html__'):
return obj.__html__()
return Markup(unicode(obj)
.replace('&', '&')