views:

32

answers:

1

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!

+2  A: 

I think I would refactor it even more an have a method, perhaps private, to produce each of the parameter tags, reducing the code duplication, otherwise I prefer the StringBuilder.

private static string Param( this HtmlHelper helper, string name, string value )
{
    var tagBuilder = new TagBuilder("param"); 
    tagBuilder .MergeAttribute("name", name); 
    tagBuilder .MergeAttribute("value", value);
    return tagBuilder.ToString( TagRenderMode.Normal );
}

public static string YouTube(this HtmlHelper helper, string youtubeId, string width, string height) 
{ 
    const string vidSuffix = "&amp;hl=en_US&amp;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 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 = helper.Param( "movie", url ) + 
                           helper.Param( "allowFullScreen", "true" ) + 
                           helper.Param( "allowscriptaccess", "always" ) + 
                           embedBuilder.ToString(TagRenderMode.Normal); 

    return objBuilder.ToString(TagRenderMode.Normal); 
}
tvanfosson
Excellent, I was on my way to something very similar! Thanks.
Anders
By the way, is the `this HtmlHelper helper` necessary in the Helper method?
Anders
@Anders - not strictly, though if you were to make it public then it would need it to conform to the pattern.
tvanfosson