Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to be able to have this kind of syntax:</p> <pre><code>using (var w = new HtmlTextWriter(sw)) { w.Html() .Head() .Script() .Attributes(new { type = "text/javascript", src = "somescript.cs" }) .WriteContent("var foo='bar'") .EndTag() .EndTag() .Body() .P() .WriteContent("some content") .EndTag() .EndTag() .EndTag(); } </code></pre> <p>In order to acheive this I've added extension methods to the HtmlTextWriter although a container would probably be more appropriate (I was more interested in getting it to work first of all!) Feeling lazy, I didn't want to write a method for each of the available tags, so I codegend the methods using a t4 template by iterating through the System.Web.UI.HtmlTextWriterTag enum. Tag attributes are managed using anonymous objects; the code basically reflects on the anonymous type, pulls out the properties and turns them into attributes which I think gives the resultant syntax a very clean appearance.</p> <p>The codegend result:</p> <pre><code>using System; using System.Web.UI; using System.Collections.Generic; /// &lt;summary&gt; /// Extensions for HtmlTextWriter /// &lt;/summary&gt; public static partial class HtmlWriterTextTagExtensions { static Stack&lt;Tag&gt; tags = new Stack&lt;Tag&gt;(); /// &lt;summary&gt; /// Opens a Unknown Html tag /// &lt;/summary&gt; public static HtmlTextWriter Unknown(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("Unknown", null)); return writer; } /// &lt;summary&gt; /// Opens a A Html tag /// &lt;/summary&gt; public static HtmlTextWriter A(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("a", null)); return writer; } /// &lt;summary&gt; /// Opens a Acronym Html tag /// &lt;/summary&gt; public static HtmlTextWriter Acronym(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("acronym", null)); return writer; } /// &lt;summary&gt; /// Opens a Address Html tag /// &lt;/summary&gt; public static HtmlTextWriter Address(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("address", null)); return writer; } /// &lt;summary&gt; /// Opens a Area Html tag /// &lt;/summary&gt; public static HtmlTextWriter Area(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("area", null)); return writer; } /// &lt;summary&gt; /// Opens a B Html tag /// &lt;/summary&gt; public static HtmlTextWriter B(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("b", null)); return writer; } /// &lt;summary&gt; /// Opens a Base Html tag /// &lt;/summary&gt; public static HtmlTextWriter Base(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("base", null)); return writer; } /// &lt;summary&gt; /// Opens a Basefont Html tag /// &lt;/summary&gt; public static HtmlTextWriter Basefont(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("basefont", null)); return writer; } /// &lt;summary&gt; /// Opens a Bdo Html tag /// &lt;/summary&gt; public static HtmlTextWriter Bdo(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("bdo", null)); return writer; } /// &lt;summary&gt; /// Opens a Bgsound Html tag /// &lt;/summary&gt; public static HtmlTextWriter Bgsound(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("bgsound", null)); return writer; } /// &lt;summary&gt; /// Opens a Big Html tag /// &lt;/summary&gt; public static HtmlTextWriter Big(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("big", null)); return writer; } /// &lt;summary&gt; /// Opens a Blockquote Html tag /// &lt;/summary&gt; public static HtmlTextWriter Blockquote(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("blockquote", null)); return writer; } /// &lt;summary&gt; /// Opens a Body Html tag /// &lt;/summary&gt; public static HtmlTextWriter Body(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("body", null)); return writer; } /// &lt;summary&gt; /// Opens a Br Html tag /// &lt;/summary&gt; public static HtmlTextWriter Br(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("br", null)); return writer; } /// &lt;summary&gt; /// Opens a Button Html tag /// &lt;/summary&gt; public static HtmlTextWriter Button(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("button", null)); return writer; } /// &lt;summary&gt; /// Opens a Caption Html tag /// &lt;/summary&gt; public static HtmlTextWriter Caption(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("caption", null)); return writer; } /// &lt;summary&gt; /// Opens a Center Html tag /// &lt;/summary&gt; public static HtmlTextWriter Center(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("center", null)); return writer; } /// &lt;summary&gt; /// Opens a Cite Html tag /// &lt;/summary&gt; public static HtmlTextWriter Cite(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("cite", null)); return writer; } /// &lt;summary&gt; /// Opens a Code Html tag /// &lt;/summary&gt; public static HtmlTextWriter Code(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("code", null)); return writer; } /// &lt;summary&gt; /// Opens a Col Html tag /// &lt;/summary&gt; public static HtmlTextWriter Col(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("col", null)); return writer; } /// &lt;summary&gt; /// Opens a Colgroup Html tag /// &lt;/summary&gt; public static HtmlTextWriter Colgroup(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("colgroup", null)); return writer; } /// &lt;summary&gt; /// Opens a Dd Html tag /// &lt;/summary&gt; public static HtmlTextWriter Dd(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("dd", null)); return writer; } /// &lt;summary&gt; /// Opens a Del Html tag /// &lt;/summary&gt; public static HtmlTextWriter Del(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("del", null)); return writer; } /// &lt;summary&gt; /// Opens a Dfn Html tag /// &lt;/summary&gt; public static HtmlTextWriter Dfn(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("dfn", null)); return writer; } /// &lt;summary&gt; /// Opens a Dir Html tag /// &lt;/summary&gt; public static HtmlTextWriter Dir(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("dir", null)); return writer; } /// &lt;summary&gt; /// Opens a Div Html tag /// &lt;/summary&gt; public static HtmlTextWriter Div(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("div", null)); return writer; } /// &lt;summary&gt; /// Opens a Dl Html tag /// &lt;/summary&gt; public static HtmlTextWriter Dl(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("dl", null)); return writer; } /// &lt;summary&gt; /// Opens a Dt Html tag /// &lt;/summary&gt; public static HtmlTextWriter Dt(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("dt", null)); return writer; } /// &lt;summary&gt; /// Opens a Em Html tag /// &lt;/summary&gt; public static HtmlTextWriter Em(this HtmlTextWriter writer) { WritePreceeding(writer); tags.Push(new Tag("em", null)); return writer; } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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