Search code examples
c#language-designt4iif

Is there a better way than 'iif' or '?:' for this kind of choosing?


I'm at a point in my custom view engine that I want things to be a bit cleaner. One of the ugliest parts I have so far is iif type functionality. It ends up looking like this:

{= CssClass==null ? "" : "class=\""+CssClass+"\"" =}

which compiles(it's a T4 template, so it just generates C# code) to the equivalent of

Write(CssClass==null ? "" : "class=\""+CssClass+"\"");

One way of shortening this I can think of is the ?? operator, but that doesn't help when you have something like Comments.Count==0

I have mostly full control over the generated C# code and what the syntax looks like in a view, so what would be a better way to do this in my design? I'm aiming for something more readable, but also fairly concise. So what syntax modifications should I allow in my view engine to make it looks better?


Solution

  • Can T4 make use of extension methods?

    This is a bit nasty, but it will make the code above a bit more readable. We can make use of the fact that extension methods work even on null references:

    public static class MyStringExtensions
    {
        public static string ToClassAttribute(this string s)
        {
            return String.IsNullOrWhiteSpace(s) ? String.Empty : "class=\"" + s + "\"";
        }
    }
    

    So now your code looks like this:

    {= CssClass.ToClassAttribute() =}
    

    If you find that this is too specific and you're doing a lot of HTML attributes as strings like this, you could make a slightly more general version:

    public static class MyStringExtensions
    {
        public static string ToAttribute(this string s, string attribute)
        {
            return String.IsNullOrWhiteSpace(s) ? String.Empty : attribute + "=\"" + s + "\"";
        }
    }
    

    So you could do this sort of thing:

    {= CssClass.ToAttribute("class") =}
    {= CssStyle.ToAttribute("style") =}
    

    etc.