Changeset 16

Show
Ignore:
Timestamp:
Thu Jan 5 08:38:51 2006
Author:
rdelon
Message:
Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/cherrytemplate.py

    r15 r16  
    23 23 default_return_generator = False  
    24 24  
      25 use_caching = False  
    25 26 _cache = {}  
    26 27  
     
    110 111         f.write( tab + "yield " + unicodePrefix + _quote3 + str + _quote3 + "\n")  
    111 112  
    112   def _getTemplateFile(filename, path):  
      113 def _get_template_file(filename, path):  
    112 113     if os.path.isabs(filename):  
    113 114         return open(filename, 'rb').read()  
     
    121 122      
    122 123  
    123   def _expandPyInclude(template, path, loop=0):  
      124 def _expand_py_include(template, path, loop=0):  
    123 124     if loop>100:  
    124 125         raise ParseError, "Infinite loop in 'py-include'"  
     
    132 133     # Read template file  
    133 134     templateFilename = template[i+12:j]  
    134       templateData = _getTemplateFile(templateFilename, path)  
      135     templateData = _get_template_file(templateFilename, path)  
    134 135  
    135 136     # Replace py-include tag with template file  
    136       if i>0 and template[i-1]=='<':  
      137     if i > 0 and template[i-1] == '<':  
    136 137         # CGTL tag (<py-include)  
    137           i=i-1  
    138           j=end  
      138         i = i-1  
      139         j = end  
    139 140     else:  
    140           if i>=5 and template[i-5:i]=='<div ':  
      141         if i >= 5 and template[i-5:i]=='<div ':  
    140 141             # CHTL tag (<div py-include)  
    141               j=_findClosingDiv(template, i, templateFilename)  
    142               i=i-5  
    143               j=j+5  
      142             j = _findClosingDiv(template, i, templateFilename)  
      143             i = i-5  
      144             j = j+5  
    144 145         else:  
    145 146             raise ParseError, "'py-include' tag can be used either as '<py-include=\"...\">' or '<div py-include=\"...\">...</div>'"  
    146 147  
    147 148     template = template[:i] + templateData + template[j+1:]  
    148       template=_expandPyInclude(template, path, loop+1)  
      149     template = _expand_py_include(template, path, loop+1)  
    148 149     return template  
    149 150  
     
    459 460  
    460 461     cache_key = template or file  
    461       _render_template_code = _cache.get(cache_key)  
      462     _expanded_template, _compiled_template = _cache.get(cache_key, (None, None))  
    461 462  
    462       if not _render_template_code:  
    463           originalTemplate = template  
      463     if not _compiled_template:  
    464 464         if file != None:  
    465               template = _getTemplateFile(file, path)  
      465             template = _get_template_file(file, path)  
      466         _expanded_template = template  
    466 467         # Expand py-include  
    467           template = _expandPyInclude(template, path)  
    468           template = template.replace('\r\n', '\n')  
      468         _expanded_template = _expand_py_include(_expanded_template, path)  
      469         _expanded_template = _expanded_template.replace('\r\n', '\n')  
    469 470         f = StringIO.StringIO()  
    470 471         f.write("def _render_template():\n")  
    471           _writeTemplate(f, template, '    ')  
    472           _render_template_code = f.getvalue()  
    473           _cache[cache_key] = _render_template_code  
      472         _writeTemplate(f, _expanded_template, '    ')  
      473         _expanded_template = f.getvalue()  
      474         try:  
      475             _compiled_template = compile(_expanded_template, '<string>', 'exec')  
      476         except:  
      477             # In case of an exception, we include the body of the template in  
      478             #   the traceback  
      479             import sys, traceback  
      480             tb = "".join(traceback.format_exception(*sys.exc_info()))  
      481             errors = ["An error occured while trying to render a template."]  
      482             if file is not None:  
      483                 errors.append("The template file was %s" % repr(file))  
      484             errors.append("The traceback was:")  
      485             errors.append(_indentAndNumberCode(tb, number = False))  
      486             errors.append("The template code was:")  
      487             errors.append(_indentAndNumberCode(_expanded_template))  
      488             if file is None:  
      489                 errors.append("The original template was:")  
      490                 errors.append(_indentAndNumberCode(template, number = False))  
      491             raise RenderError, '\n'.join(errors)  
      492  
      493         if use_caching:  
      494             _cache[cache_key] = _compiled_template, _expanded_template  
    474 495  
    475 496     if loc is None:  
     
    482 503      
    483 504     try:  
    484           exec(_render_template_code, g)  
      505         exec(_compiled_template, g)  
    484 505     except:  
    485 506         # In case of an exception, we include the body of the template in  
     
    488 509         import sys, traceback  
    489 510         tb = "".join(traceback.format_exception(*sys.exc_info()))  
    490           errorList = ["An error occured while trying to render a template."]  
      511         errors = ["An error occured while trying to render a template."]  
    490 511         if file is not None:  
    491               errorList.append("The template file was %s" % repr(file))  
    492           errorList.append("The traceback was:")  
    493           errorList.append(_indentAndNumberCode(tb, number = False))  
    494           errorList.append("The template code was:")  
    495           errorList.append(_indentAndNumberCode(template))  
      512             errors.append("The template file was %s" % repr(file))  
      513         errors.append("The traceback was:")  
      514         errors.append(_indentAndNumberCode(tb, number = False))  
      515         errors.append("The template code was:")  
      516         errors.append(_indentAndNumberCode(_expanded_template))  
    496 517         if file is None:  
    497               errorList.append("The original template was:")  
    498               errorList.append(_indentAndNumberCode(originalTemplate, number = False))  
    499           raise RenderError, '\n'.join(errorList)  
      518             errors.append("The original template was:")  
      519             errors.append(_indentAndNumberCode(template, number = False))  
      520         raise RenderError, '\n'.join(errors)  
    500 521  
    501 522     if output_encoding == None:  
     
    531 552         import sys, traceback  
    532 553         tb = "".join(traceback.format_exception(*sys.exc_info()))  
    533           errorList = ["An error occured while trying to render a template."]  
      554         errors = ["An error occured while trying to render a template."]  
    533 554         if file is not None:  
    534               errorList.append("The template file was %s" % repr(file))  
    535           errorList.append("The traceback was:")  
    536           errorList.append(_indentAndNumberCode(tb, number = False))  
    537           errorList.append("The template code was:")  
    538           errorList.append(_indentAndNumberCode(template))  
      555             errors.append("The template file was %s" % repr(file))  
      556         errors.append("The traceback was:")  
      557         errors.append(_indentAndNumberCode(tb, number = False))  
      558         errors.append("The template code was:")  
      559         errors.append(_indentAndNumberCode(_expanded_template))  
    539 560         if file is None:  
    540               errorList.append("The original template was:")  
    541               errorList.append(_indentAndNumberCode(originalTemplate, number = False))  
    542           raise RenderError, '\n'.join(errorList)  
      561             errors.append("The original template was:")  
      562             errors.append(_indentAndNumberCode(template, number = False))  
      563         raise RenderError, '\n'.join(errors)  
    543 564  
    544 565 renderTemplate = render # legacy API