Thanks Massimo.
That prints out:
hihihi
If you change it to:
{{for d in data[:3]:
= "hi\n"
pass}}
It prints out (spacing gets messed up):
hi
hi
hi
Anyway, you probably aren't interested in a patch (see attached), but this
patch has been working pretty good for me when using my original syntax.
Although, I don't know if there are any unforeseen consequences since I'm
not using the templating engine to it's full potential right now.
Thanks,
Rob
On Tuesday, July 10, 2012 8:07:16 PM UTC-7, Massimo Di Pierro wrote:
>
> You can do
>
> {{for d in data[:3]:
> = "hi"
> pass}}
>
> to achieve when you want.
>
> On Tuesday, 10 July 2012 16:08:40 UTC-5, Rob wrote:
>>
>> Hi,
>>
>> I'm using the standalone template.py to generate non html files and the
>> excess whitespace is making this somewhat painful.
>>
>> For example, my view:
>> #ifndef __BLAH__
>> #define __BLAH__
>> {{for d in data[:3]:}}
>> {{= "hi"}}
>> {{pass}}
>> #endif
>>
>> outputs:
>> #ifndef __BLAH__
>> #define __BLAH__
>>
>> hi
>>
>> hi
>>
>> hi
>>
>> #endif
>>
>> I'm sure there are lots of reasons why this can't happen, but I propose
>> that render() simply removes lines that contain nothing but whitespace and
>> logic. For example, given the same view:
>> 1 #ifndef __BLAH__
>> 2 #define __BLAH__
>> 3 {{for d in data[:3]:}}
>> 4 {{= "hi"}}
>> 5 {{pass}}
>> 6 #endif
>>
>> Line number 3 and 6 contain nothing but template logic and whitespace
>> (with excess carriage returns). Is there a reason why the rendering engine
>> couldn't simply remove this whitespace from the output? IE: if the line
>> contains pure logic ({{...}}) and whitespace, just remove it. If the line
>> contains {{=..}} and other whitespace then it stays.
>>
>> Does this break all sorts of HTML output?
>>
>> Thanks,
>> Rob
>>
>
--- template.orig.py Wed Jul 11 11:16:57 2012
+++ template.py Wed Jul 11 11:27:33 2012
@@ -234,6 +234,7 @@
default_delimiters = ('{{','}}')
r_tag = re.compile(r'(\{\{.*?\}\})', re.DOTALL)
+ r_whitespace_cleanup = re.compile(r'^\s*(\{\{[^=].*?\}\})\s*\n', re.MULTILINE)
r_multiline = re.compile(r'(""".*?""")|(\'\'\'.*?\'\'\')', re.DOTALL)
@@ -546,6 +547,9 @@
in_tag = False
extend = None
pre_extend = True
+
+ # cleanup unwanted whitespace around template logic
+ text = re.sub(self.r_whitespace_cleanup, "\g<1>", text)
# Use a list to store everything in
# This is because later the code will "look ahead"