views:

10

answers:

1

I have a template fragment that looks like this:

<#+
if (length == "0") 
#>  return record.Substring(offset);
<#+
else 
#>  return record.Substring(offset, <#= length #>);

When length != "0" it works fine, but when it is "0" it emits the record.Substring(offset); code ok but is then followed by the text "0);" (without the double-quotes) on the next line. It looks like it is emitting the fragment "<#= length #>);" from the else block. I don't understand why?

A: 

You should always use brackets in T4.

return record.Substring(offset, <#= length #>);

translates to something like

Write("return record.Substring(offset, ");
Write(length);
Write(");");

This is why "else" outputs only the first part.

Your code should be like this:

<#+ if (length == "0") { #>
  return record.Substring(offset);
<#+ } else { #>
  return record.Substring(offset, <#= length #>);
<#+ } #>
Athari