jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 78 forks source link

whitespace control #25

Open seanmiddleditch opened 10 years ago

seanmiddleditch commented 10 years ago

From experience writing a lot of code-generation sorts of templates (e.g. generating C++ or C# code, not HTML "code"), a useful variation of this feature is to allow a block to have whitespace compressed similar to HTML's rules, allowing markup like:

var {{name}} = {{compress-whitespace}}
  {{if is_string}}
    "{{value}}"
  {{else}}
     {{value}}
  {{end}}
  ;{{/compress-whitespace}}

which would produce (for name=foo and is_string=true and value=bar)

  var foo = "bar";

The ability to strip out the line of any control tag (if/else/end) that is nothing but whitespace is also useful to avoid a bunch of empty lines showing up in the output.

It's also handy to have a way to output the current input line number and filename so you can insert #line tags and the like.