Updated patch - I haven't been able to break it.  I'm posting this in case 
someone else finds it useful - it's a very small patch.

This basically looks for {{...}} and strips the white space around it (up 
to an including a new line) ignoring everything else.  The output from 
render() now looks just like the view code without a bunch of extra white 
space and carriage returns.

Thanks a lot for making this template library available to use outside of 
web2py!

On Wednesday, July 11, 2012 11:33:44 AM UTC-7, Rob wrote:
>
> 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.
>
> UPDATE: the attached patch matches template lines like    {{for d in 
> data[:3]:}}{{foo = 5}}{{=foo}}   which it shouldn't (I think) and my regex 
> foo is weak :/
>
> 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 15:03:32 2012
@@ -234,6 +234,7 @@
 
     default_delimiters = ('{{','}}')
     r_tag = re.compile(r'(\{\{.*?\}\})', re.DOTALL)
+    r_whitespace_cleanup = re.compile(r' *?(\{\{([^=])(.*?)\}\})[^\S\n]*\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"

Reply via email to