| 107 | === What is Genshi doing with the whitespace in my markup template? === |
| 108 | |
| 109 | When you serialize to XML/HTML, Genshi by default attempts to remove excessive whitespace: basically, it strips all trailing spaces and remove any empty lines. This is done to help keep down the size of generated content to be transmitted over the web. Genshi will not do any reformatting (such as indenting tags), as that would be too expensive and often of no practical use. |
| 110 | |
| 111 | If you're rendering XHTML or HTML output, Genshi tries not to mess with certain tags that are known to be sensitive to whitespace (such as `<textarea>` and `<pre>`). However, in general it is not trivial to determine whether whitespace is significant or not in markup, so you ''may'' see unexpected results in some cases. If that is a problem, the simplest solution is to disable stripping of whitespace entirely (a side effect is that serialization will be sped up): |
| 112 | |
| 113 | {{{ |
| 114 | #!python |
| 115 | stream.render('html', strip_whitespace=False) |
| 116 | }}} |
| 117 | |
| 118 | If you'd like to keep the whitespace removal in general, but need to disable it inside certain elements, you can use the [http://www.w3.org/TR/REC-xml/#sec-white-space xml:space] attribute: |
| 119 | |
| 120 | {{{ |
| 121 | #!xml |
| 122 | … whitespace is stripped here … |
| 123 | |
| 124 | <div xml:space="preserve"> |
| 125 | |
| 126 | … whitespace is kept intact inside this tag … |
| 127 | |
| 128 | </div> |
| 129 | |
| 130 | … whitespace is stripped here, again … |
| 131 | }}} |
| 132 | |
| 133 | You'll need to do that when: |
| 134 | 1. you're rendering to XML |
| 135 | 2. you're rendering to XHTML/HTML, and have set the element to display as `whitespace: nowrap` via CSS |
| 136 | 3. you have `<pre>` or `<textarea>` tags that are embedded as opaque `Markup` instances, in which case Genshi never actually sees the tags (see #78) |
| 137 | |