Using template functions cause errors.

Issue #11 new
Clay Sweetser
created an issue

(Due to lack of documentation examples, the below usage may be wrong, but it's the best I could infer) Using an inline function:

from wheezy.template.engine import Engine
from wheezy.template.ext.core import CoreExtension
from wheezy.template.loader import DictLoader

func_block = """\
@requires(boolean)
@def yes_no(boolean):
@if boolean:
Yes
@else:
No
@end
@end
"""

test_block = """\
@requires(p)
@import 'func_block' as func
My answer is @func.yes_no(p)
"""

template_dict = {
    'func_block': func_block,
    'test_block': test_block
}

engine = Engine(
    loader=DictLoader(template_dict),
    extensions=[CoreExtension()]
)

template = engine.get_template('test_block')
print(template.render({'p':True}))

produces the error

Traceback (most recent call last):
  File "test.py", line 32, in <module>
    template = engine.get_template('test_block')
  File "C:\Python27\lib\site-packages\wheezy.template-0.1.151-py2.7.egg\wheezy\template\engine.py", line 41, in get_template
    self.compile_template(name)
  File "C:\Python27\lib\site-packages\wheezy.template-0.1.151-py2.7.egg\wheezy\template\engine.py", line 89, in compile_template
    source, name)['render']
  File "C:\Python27\lib\site-packages\wheezy.template-0.1.151-py2.7.egg\wheezy\template\compiler.py", line 26, in compile_source
    source = adjust_source_lineno(source, name, self.source_lineno)
  File "C:\Python27\lib\site-packages\wheezy.template-0.1.151-py2.7.egg\wheezy\template\comp.py", line 21, in adjust_source_lineno
    source = compile(source, name, 'exec', ast.PyCF_ONLY_AST)
  File "test_block", line 3
    (p = ctx['(p']
       ^
SyntaxError: invalid syntax

Comments (6)

  1. Andriy Kornatskyy repo owner

    changes made to original source:

    6d5
    < @requires(boolean)
    7a7
    > @#ignore
    17c17
    < @requires(p)
    ---
    > @require(p)
    

    and all together:

    from wheezy.template.engine import Engine
    from wheezy.template.ext.core import CoreExtension
    from wheezy.template.loader import DictLoader
    
    func_block = """\
    @def yes_no(boolean):
    @#ignore
    @if boolean:
    Yes
    @else:
    No
    @end
    @end
    """
    
    test_block = """\
    @require(p)
    @import 'func_block' as func
    My answer is @func.yes_no(p)
    """
    
    template_dict = {
        'func_block': func_block,
        'test_block': test_block
    }
    
    engine = Engine(
        loader=DictLoader(template_dict),
        extensions=[CoreExtension()]
    )
    
    template = engine.get_template('test_block')
    print(template.render({'p':True}))
    
  2. Andriy Kornatskyy repo owner

    It is a workaround. If you remove that line you will get an issue like this:

    Traceback (most recent call last):
      File "test.py", line 33, in <module>
        print(template.render({'p':True}))
      File "/Users/ak/env/lib/python2.7/site-packages/wheezy.template-0.1.151-py2.7.egg/wheezy/template/engine.py", line 134, in render
        return self.render_template(ctx, {}, {})
      File "test_block", line 3, in render
      File "func_block", line 2, in yes_no
    SyntaxError: The compound statement 'if' is not allowed here. Add a line before it with @#ignore or leave it empty.
    
    def yes_no(boolean):
        @#ignore
        @if ...
    

    The template is directly translated to python code trying to preserve line numbering in python code that corresponds directly to template code. The use of compound statement (e.g. if) at the beginning of function does not let engine "insert" a buffer for generated strings so as a workaround it ask you to add that line to your template code that will be used by engine as a placeholder for python code to create a buffer.

  3. Log in to comment