Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what I came up with, taking care of the following considerations:</p> <ol> <li>I save some typing with <code>T.Tag</code> after <code>using T = HtmlTextWriterTag;</code>, which you might like or not</li> <li>I wanted to get at least some safety for the sequence of the invocation chain (the <code>Debug.Assert</code> is just for brevity, the intention should be clear)</li> <li><p>I didn't want to wrap the myriad of methods of the HtmlTextWriter.</p> <pre><code>using T = HtmlTextWriterTag; public class HtmlBuilder { public delegate void Statement(HtmlTextWriter htmlTextWriter); public HtmlBuilder(HtmlTextWriter htmlTextWriter) { this.writer = htmlTextWriter; } // Begin statement for tag; mandatory, 1st statement public HtmlBuilder B(Statement statement) { Debug.Assert(this.renderStatements.Count == 0); this.renderStatements.Add(statement); return this; } // Attribute statements for tag; optional, 2nd to nth statement public HtmlBuilder A(Statement statement) { Debug.Assert(this.renderStatements.Count &gt; 0); this.renderStatements.Insert(this.cntBeforeStatements++, statement); return this; } // End statement for tag; mandatory, last statement // no return value, fluent block should stop here public void E() { Debug.Assert(this.renderStatements.Count &gt; 0); this.renderStatements.Add(i =&gt; { i.RenderEndTag(); }); foreach (Statement renderStatement in this.renderStatements) { renderStatement(this.writer); } this.renderStatements.Clear(); this.cntBeforeStatements = 0; } private int cntBeforeStatements = 0; private readonly List&lt;Statement&gt; renderStatements = new List&lt;Statement&gt;(); private readonly HtmlTextWriter writer; } public class HtmlWriter { public delegate void BlockWithHtmlTextWriter(HtmlTextWriter htmlTextWriter); public delegate void BlockWithHtmlBuilder(HtmlBuilder htmlBuilder); public string Render(BlockWithHtmlTextWriter block) { StringBuilder stringBuilder = new StringBuilder(); using (StringWriter stringWriter = new StringWriter(stringBuilder)) { using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter)) { block(htmlTextWriter); } } return stringBuilder.ToString(); } public string Render(BlockWithHtmlBuilder block) { return this.Render((HtmlTextWriter htmlTextWriter) =&gt; block(new HtmlBuilder(htmlTextWriter))); } // small test/sample static void Main(string[] args) { HtmlWriter htmlWriter = new HtmlWriter(); System.Console.WriteLine(htmlWriter.Render((HtmlBuilder b) =&gt; { b.B(h =&gt; h.RenderBeginTag(T.Div) ) .A(h =&gt; h.AddAttribute("foo", "bar") ) .A(h =&gt; h.AddAttribute("doh", "baz") ) .E(); })); } } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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