Mercurial > genshi > mirror
diff genshi/template/base.py @ 610:5e358de79e4c trunk
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
* If an include is found when parsing a template, but no template loader has been specified, a `TemplateSyntaxError` is raised.
author | cmlenz |
---|---|
date | Mon, 27 Aug 2007 23:20:47 +0000 |
parents | 6d4877844e28 |
children | 7e666e3e4f1b |
line wrap: on
line diff
--- a/genshi/template/base.py +++ b/genshi/template/base.py @@ -39,7 +39,7 @@ class TemplateError(Exception): """Base exception class for errors related to template processing.""" - def __init__(self, message, filename='<string>', lineno=-1, offset=-1): + def __init__(self, message, filename=None, lineno=-1, offset=-1): """Create the exception. :param message: the error message @@ -48,6 +48,8 @@ occurred :param offset: the column number at which the error occurred """ + if filename is None: + filename = '<string>' self.msg = message #: the error message string if filename != '<string>' or lineno >= 0: message = '%s (%s, line %d)' % (self.msg, filename, lineno) @@ -62,7 +64,7 @@ error, or the template is not well-formed. """ - def __init__(self, message, filename='<string>', lineno=-1, offset=-1): + def __init__(self, message, filename=None, lineno=-1, offset=-1): """Create the exception :param message: the error message @@ -84,7 +86,7 @@ with a local name that doesn't match any registered directive. """ - def __init__(self, name, filename='<string>', lineno=-1): + def __init__(self, name, filename=None, lineno=-1): """Create the exception :param name: the name of the directive @@ -333,6 +335,10 @@ self.lookup = lookup self.allow_exec = allow_exec + self.filters = [self._flatten, self._eval, self._exec] + if loader: + self.filters.append(self._include) + if isinstance(source, basestring): source = StringIO(source) else: @@ -341,9 +347,6 @@ self.stream = list(self._prepare(self._parse(source, encoding))) except ParseError, e: raise TemplateSyntaxError(e.msg, self.filepath, e.lineno, e.offset) - self.filters = [self._flatten, self._eval, self._exec] - if loader: - self.filters.append(self._include) def __repr__(self): return '<%s "%s">' % (self.__class__.__name__, self.filename) @@ -386,7 +389,7 @@ yield event else: if kind is INCLUDE: - href, fallback = data + href, cls, fallback = data if isinstance(href, basestring) and \ not getattr(self.loader, 'auto_reload', True): # If the path to the included template is static, and @@ -394,7 +397,7 @@ # the template is inlined into the stream try: tmpl = self.loader.load(href, relative_to=pos[0], - cls=self.__class__) + cls=cls or self.__class__) for event in tmpl.stream: yield event except TemplateNotFound: @@ -513,7 +516,7 @@ for event in stream: if event[0] is INCLUDE: - href, fallback = event[1] + href, cls, fallback = event[1] if not isinstance(href, basestring): parts = [] for subkind, subdata, subpos in self._eval(href, ctxt): @@ -522,7 +525,7 @@ href = u''.join([x for x in parts if x is not None]) try: tmpl = self.loader.load(href, relative_to=event[2][0], - cls=self.__class__) + cls=cls or self.__class__) for event in tmpl.generate(ctxt): yield event except TemplateNotFound: