<br><div class="gmail_quote">On Sun, Dec 11, 2011 at 9:21 PM, Greg Ewing <span dir="ltr"><<a href="mailto:greg.ewing@canterbury.ac.nz" target="_blank">greg.ewing@canterbury.ac.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Mike Meyer wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
With significant whitespace, you have to keep explicit track of the<br>
current nesting level in order to generate the appropriate<br>
indent. With real delimiters, you can drop that.<br>
</blockquote></blockquote>
<br></div>
Yes, but with an appropriate design you can arrange for that to<br>
be done in only *one* place. There's no need for every method<br>
that generates code to know about indentation levels.<br></blockquote><div><br></div><div>I've written this code (for HTML) and it's not only straightforward, it makes the code easier to write and more robust. And you can make the closing automatic with a context manager. Here's how you might write some HTML:</div>
<div><br></div><div> with html_tag('table'):</div><div> with html_tag('tr'):</div><div> for x in data:</div><div> with html_tag('td'):</div><div> # do stuff with x</div>
<div><br></div><div>On the other hand, this might not help with template languages as Nick points out:</div><div><br><div class="gmail_quote">On Sun, Dec 11, 2011 at 2:30 AM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><% if danger_level > 3 {: %></div>
<div class="alert"><% if danger_level == 5 {: %>EXTREME <% :}<br>%>DANGER ALERT!</div><br><br><% elif danger_level > 0 {: %><br> <div>Some chance of danger</div><br>
<br><% else {: %><br> <div>No danger</div><br><br><% :} %><br><br><% for a in ['cat', 'dog', 'rabbit'] {: %><br> <h2><%= a %></h2><br> <p><%= describe_animal(a) %></p><br>
<br><% :} %></blockquote><div><br></div><div> While I thought his example was compelling at first, after thinking it through I realized that any existing template language that generates Python has to be aware of indentation and it's going to have to modify the indentation of the included Python code. So it would need to either read the indentation or read the {: and :} tokens. So what if we think in terms of modifying the template language instead of Python? In the code below, I've added two extra tokens to the template language: %{> which increases the indentation and <}% which decreases it. All other leading spaces would be stripped. Here's that example rewritten:</div>
<div><br></div><div><% if danger_level > 3: %{></div><div> <div class="alert"><% if danger_level == 5: %{>EXTREME <}%</div><div>%>DANGER ALERT!</div></div><div><br></div><div><% elif danger_level > 0: %{></div>
<div> <div>Some chance of danger</div></div><div><br></div><div><% else: %{></div><div> <div>No danger</div></div><div><br></div><div><}%></div><div><br></div><div><% for a in ['cat', 'dog', 'rabbit']: %{></div>
<div> <h2><%= a %></h2></div><div> <p><%= describe_animal(a) %></p></div><div><br></div><div><}%></div><div><br></div><div>This isn't a perfect design. In particular %{> wouldn't be necessary if the template language parses the embedded code, but it's sufficient to illustrate the point.</div>
<div><br></div></div></div><font face="arial, helvetica, sans-serif">--- Bruce</font><div><font face="arial, helvetica, sans-serif">Follow me: <a href="http://www.twitter.com/Vroo" target="_blank">http://www.twitter.com/Vroo</a> <a href="http://www.vroospeak.com/" target="_blank">http://www.vroospeak.com</a></font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"><br></font></div></div>