marrow / cinje

A Pythonic and ultra fast template engine DSL.
MIT License
32 stars 0 forks source link

Correctly handle edge case were first template text is conditional. #14

Closed amcgregor closed 8 years ago

amcgregor commented 8 years ago

The following example produces strangeness:

: def template
    : if False
        Hi.

This generates the following code (pre-1.0):

def template(*, _escape=_escape, _bless=_bless, _args=_args):
    if False:
        _buffer = []
        __w, __ws = _buffer.extend, _buffer.append

        __ws('            Hi.\n')

    yield "".join(_buffer)

The _buffer allocation being itself conditional results in problems. Lame, naive solution: have all template functions call ensure_buffer immediately.

amcgregor commented 8 years ago

Resolved by ensuring the buffer is immediately recreated upon flush, rather than relying on deferred reconstruction.