return arch
class QWebContext(dict):
- def __init__(self, cr, uid, data, loader=None, templates=None, context=None):
+ def __init__(self, cr, uid, data, loader=None, context=None):
self.cr = cr
self.uid = uid
self.loader = loader
- self.templates = templates or {}
self.context = context
dic = dict(data)
super(QWebContext, self).__init__(dic)
"""
return QWebContext(self.cr, self.uid, dict.copy(self),
loader=self.loader,
- templates=self.templates,
context=self.context)
def __copy__(self):
def register_tag(self, tag, func):
self._render_tag[tag] = func
- def add_template(self, qwebcontext, name, node):
- """Add a parsed template in the context. Used to preprocess templates."""
- qwebcontext.templates[name] = node
-
- def load_document(self, document, res_id, qwebcontext):
- """
- Loads an XML document and installs any contained template in the engine
+ def get_template(self, name, qwebcontext):
+ origin_template = qwebcontext.get('__caller__') or qwebcontext['__stack__'][0]
+ try:
+ document = qwebcontext.loader(name)
+ except ValueError:
+ raise_qweb_exception(QWebTemplateNotFound, message="Loader could not find template %r" % name, template=origin_template)
- :type document: a parsed lxml.etree element, an unparsed XML document
- (as a string) or the path of an XML file to load
- """
- if not isinstance(document, basestring):
- # assume lxml.etree.Element
+ if hasattr(document, 'documentElement'):
dom = document
elif document.startswith("<?xml"):
dom = etree.fromstring(document)
else:
dom = etree.parse(document).getroot()
+ res_id = isinstance(name, (int, long)) and name or None
for node in dom:
- if node.get('t-name'):
- name = str(node.get("t-name"))
- self.add_template(qwebcontext, name, node)
- if res_id and node.tag == "t":
- self.add_template(qwebcontext, res_id, node)
- res_id = None
-
- def get_template(self, name, qwebcontext):
- """ Tries to fetch the template ``name``, either gets it from the
- context's template cache or loads one with the context's loader (if
- any).
-
- :raises QWebTemplateNotFound: if the template can not be found or loaded
- """
- origin_template = qwebcontext.get('__caller__') or qwebcontext['__stack__'][0]
- if qwebcontext.loader and name not in qwebcontext.templates:
- try:
- xml_doc = qwebcontext.loader(name)
- except ValueError:
- raise_qweb_exception(QWebTemplateNotFound, message="Loader could not find template %r" % name, template=origin_template)
- self.load_document(xml_doc, isinstance(name, (int, long)) and name or None, qwebcontext=qwebcontext)
-
- if name in qwebcontext.templates:
- return qwebcontext.templates[name]
+ if node.get('t-name') or (res_id and node.tag == "t"):
+ return node
raise QWebTemplateNotFound("Template %r not found" % name, template=origin_template)