Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two issues that I see:</p> <ul> <li>Repeated use of <code>Tag(Tagname, …)</code>. Why not offer extension methods for each tag name? Admittedly, this bloats the interface and is quite a lot to write (=> code generation!).</li> <li>The compiler/IDE doesn't assist you. In particular, it doesn't check indentation (it will even destroy it when you indent your automatically).</li> </ul> <p>Both problems could perhaps be solved by using a Lambda approach:</p> <pre><code>writer.Write(body =&gt; new Tag[] { new Tag(h1 =&gt; "Hello, world!"), new Tag(p =&gt; "Indeed. What a lovely day.", new Attr[] { new Attr("style", "color: red") }) }); </code></pre> <p>This is just one basic approach. The API certainly would need a lot more work. In particular, nesting the same tag name won't work because of argument name conflicts. Also, this interface wouldn't work well (or at all) with VB. But then, the same is unfortunately true for other modern .NET APIs, even the PLINQ interface from Microsoft.</p> <p>Another approach that I've thought about some time ago actually tries to emulate Markaby, like sambo's code. The main difference is that I'm using <code>using</code> blocks instead of <code>foreach</code>, thus making use of RAII:</p> <pre><code>using (var body = writer.body("xml:lang", "en")) { using (var h1 = body.h1()) h1.AddText("Hello, World!"); using (var p = body.p("style", "color: red")) p.AddText("Indeed. What a lovely day."); } </code></pre> <p>This code doesn't have the problems of the other approach. On the other hand, it provides less type safety for the attributes and a less elegant interface (for a given definition of “<em>elegant</em>”).</p> <p>I get both codes to compile and even produce some more or less meaningful output (i.e.: HTML!).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload