Hello SO:
I wrote an HTML helper to generate a YouTube embed link. It has 3 parameters, YouTubeID, Width, and Height. I originally wrote it with a StringBuilder, but then I decided to try to use the TagBuilder (well, a few of them).
Here are the two different returns:
//Tag Builder
public static string YouTube(this HtmlHelper helper, string youtubeId, string width, string height)
{
const string vidSuffix = "&hl=en_US&fs=1";
var url = "http://www.youtube.com/v/" + youtubeId + vidSuffix;
var objBuilder = new TagBuilder("object");
objBuilder.MergeAttribute("width",width);
objBuilder.MergeAttribute("height",height);
var movParamBuilder = new TagBuilder("param");
movParamBuilder.MergeAttribute("name","movie");
movParamBuilder.MergeAttribute("value",url);
var fsParamBuilder = new TagBuilder("param");
fsParamBuilder.MergeAttribute("name","allowFullScreen");
fsParamBuilder.MergeAttribute("value","true");
var saParamBuilder = new TagBuilder("param");
saParamBuilder.MergeAttribute("name", "allowscriptaccess");
saParamBuilder.MergeAttribute("value", "always");
var embedBuilder = new TagBuilder("embed");
embedBuilder.MergeAttribute("src",url);
embedBuilder.MergeAttribute("type", "application/x-shockwave-flash");
embedBuilder.MergeAttribute("allowscriptaccess","always");
embedBuilder.MergeAttribute("allowfullscreen","true");
embedBuilder.MergeAttribute("width",width);
embedBuilder.MergeAttribute("height",height);
objBuilder.InnerHtml = movParamBuilder.ToString(TagRenderMode.Normal) +
fsParamBuilder.ToString(TagRenderMode.Normal) +
saParamBuilder.ToString(TagRenderMode.Normal) +
embedBuilder.ToString(TagRenderMode.Normal);
return objBuilder.ToString(TagRenderMode.Normal);
}
//StringBuilder
public static string YouTube(this HtmlHelper helper, string youtubeId, string width, string height)
{
const string vidSuffix = "&hl=en_US&fs=1";
var url = "http://www.youtube.com/v/" + youtubeId + vidSuffix;
sb.AppendFormat("<object width=\"{0}\" height=\"{1}\">", width, height);
sb.AppendLine();
sb.AppendFormat("<param name=\"movie\" value=\"{0}\">",url);
sb.AppendLine();
sb.Append("</param><param name=\"allowFullScreen\" value=\"true\">");
sb.AppendLine();
sb.AppendFormat("</param><param name=\"allowscriptaccess\" value=\"always\">");
sb.AppendLine();
sb.AppendFormat(
"</param><embed src=\"{0}\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"{1}\" height=\"{2}\">",
url, width, height);
sb.AppendLine();
sb.Append("</embed></object>");
return sb.ToString();
}
Both of them generate the exact same code, except the StringBuilder adds a line break after each section.
Any thoughts on which would be better, or if it even matters? Thanks!