views:

244

answers:

5

what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable?

aren't they generating the same thing??

+3  A: 

It's just convenience. From this tutorial:

You don’t really need to use the TagBuilder class. You could use a StringBuilder class instead. However, the TagBuilder class makes your life a little easier.

Look at the methods on TagBuilder, and think about whether they give you value. would you want to do the same thing yourself manually in StringBuilder every time? Is there escaping that it does for you? Attribute merging, etc? Is the resulting code easier to read, making it clearer that you're building a tag rather than some arbitrary string?

Jon Skeet
+9  A: 

TagBuilder is a class that specially designed for creating html tags and their content. You are right saying that result will be anyway a string and of course you still can use StringBuilder and the result will be the same, but you can do things easier with TagBuilder. Lets say you need to generate a tag:

<a href='http://www.stackoverflow.com' class='coolLink'/>

Using StringBuilder you need to write something like this:

var sb = new StringBuilder();
sb.Append("<a href='");
sb.Append(link);
sb.Append("' class = '");
sb.Append(ccsClass);
sb.Append("'/>");
sb.ToString();

It is not very cool, isn’t it? And compare how you can build it using TagBuilder;

var tb = new TagBuilder("a");
tb.MergeAttribute("href",link);
tb.AddCssClass(cssClass);
tb.ToString(TagRenderMode.SelfClosing);

Isn't that better?

Bashir Magomedov
For simple tags as per above I prefer `sb.AppendFormat("<a href='{0}' class = '{1}'/>", link, cssClass);` but the methods on TagBuilder are really neat if one needs more complex tags.
Don
Note he asked about building a table, i.e. he's probably nesting table, tbody and then multiple trs and tds. Won't he need one tagbuilder for each of those? I expect that'll get a lot messier than the equivalent stringbuilder.
Rup
"<a href='" + link + "' class='" + class + "' />" is even more cleaner. And it's fast because 1) string concatenation is quite fast, with end-result being single string anyway, and 2) don't optimize prematurely. For more complex cases (like tables) I'd suggest templating engine like Spark. I've seen sources that use TagBuilder and that's still hard to understand.
queen3
@Rup Actually if you make the right extension methods it's actually much cleaner to use a semantic model like TagBuilder for generating nested tags.
Ryan
+1  A: 

aren't they generating the same thing??

Well, sure, but that shouldn't be a deterrent, should it? One class is designed for something more specific than the other, so it offers a greater level of convenience.

I could ask: why use a StringBuilder? Why not a List<char>? Couldn't I generate the same thing from either?

Going one step further: why even a List<char>? Why not just a char[], the resizing/manipulation of which I can control myself? I can still totally create a string from a char[].

In fact, all I really need is a char* and an int (for length). Right?

My point is just that if a class is available for specialized functionality that you can use, it makes sense to use it if you ask me.

Dan Tao
A: 

Don't forget to do HTML encoding of values if you are using StringBuilder. I hope TagBuilder do this automatically.

STO
+1  A: 

There's a point that the other answers have missed so far. If you return TagBuilder from an extension method you can continue to add attributes in your view. Let's say you were returning a table from an Html helper and you want to add a class attribute. If you're using a StringBuilder you need to pass the class in as a parameter.

public static string Table(...., string @class)
{
    ...
    sb.AppendFormat("class='{0}", @class);
    ...
}

// In the view
<%: Html.Table(someParams, "fancy") %>

But adding a class attribute to an HTML tag is not the concern of an extension method that creates a table! If we switch to a semantic model (TagBuilder) for generating the HTML, we can add the class attribute outside of the table method.

public static TagBuilder Table(....)
{
    ...
    return tag;
}

// In the view
<%: Html.Table(someParams).AddCssClass("fancy") %>

In addition to TagBuilder, you might want to check out FubuMVC's HtmlTags library. It's a much better model for generating HTML. I have some more details on blog.

Ryan