Search code examples
c#stringtemplatetexttemplate

StringTemplate C# elseif bug?


I use StringTemplate version 3.

I have the following template:

<html>
  <head>
    <title>Test</title>
    <style>
        .done-ok {          
            background-color    : LawnGreen         
        }
        .done-ignore {                
            background-color    : Orange         
        }
        .done-fail {                
            background-color    : OrangeRed         
        }           
    </style>
  </head>
  <body class="$if(failedNonEmpty)$done-fail$elseif(needsAttentionNonEmpty)$done-ignore$else$done-ok$endif$">
  </body>
</html>

I use the following code:

    var stg = new StringTemplateGroup(templateName, new FileSystemTemplateLoader(Path.Combine(locationRoot, templateFolder)));
    var st = stg.GetInstanceOf(templateName);

    st.SetAttribute("passedNonEmpty", !true);
    st.SetAttribute("failedNonEmpty", !true);
    st.SetAttribute("needsAttentionNonEmpty", !true);

    var content = st.ToString();

When I compile the code everthing is ok. The strange thing is when i change the second attribute to:

st.SetAttribute("failedNonEmpty", true);

Then suddenly I get the exception on the line st.ToString() saying: no text in template 'elseif'

Anyone having the same problems or knows a solution?


Solution

  • As far as I remember there is no elseif in ST3 (and it's really pain in the ass). ST4 has it though.

    Edit:

    In fact elseif was added in 3.1 (not sure if it works)

    source: https://theantlrguy.atlassian.net/wiki/display/ST/3.1+Release+Notes