> I wanted to preserve newlines in the generated webpage. I had a
> solution with split in my template. Something like:
> #####
> <element py:strip="" py:for="line in blogEntry.entryText.split('\n')">
>     ${line}<br/>
> </element>
> #####
>
> I made the following function:
> #####
> def splitLines(thisText):
>     if thisText == None:
>         return []
>     thisText = thisText.replace('&', '&amp;')
>     thisText = thisText.replace('<', '&lt;')
>     thisText = thisText.replace('>', '&gt;')
>     return '<br/>'.join(thisText.split('\n'))
> #####

You might want to add &quot; to your list. And if in the majority of
cases thisText doesn't contain the special characters the following
will be better performance-wise:

 def splitLines(thisText):
     if thisText == None:
         return []
     if '&' in thisText: thisText = thisText.replace('&', '&amp;')
     if '<' in thisText: thisText = thisText.replace('<', '&lt;')
     if '>' in thisText: thisText = thisText.replace('>', '&gt;')
     if '"' in thisText: thisText = thisText.replace('"', '&quot;' )
     return '<br/>'.join(thisText.split('\n'))

And if this part of your code becomes the real performance bottleneck
you might want to do the search/replace using the re module. Take a
look at xml.etree.ElementTree in the stdlib for an example (if you
have python > 2.4).

> and now I can put the following in my template:
> #####
> ${XML(tg.splitLines(blogEntry.entryText))}
> #####
>
> This is more clear and I am rid of the <br/> at the end.


Cheers,
Daniel
-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to