Question

I'm currently working with T4 templates and I have noticed that sometimes the code is not indented properly, how can I avoid that?

For instance I have this code in the template

}
    <# } #>
    this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

and in the generated class it's like

}
                 this.cmbDecisionList.Dat = dataSource;
      this.btnDec.Enabled = dataSource.Count > 0;
}
Was it helpful?

Solution

Allow me to illustrate your problem by replacing spaces with dots.

}
....<# } #>
....this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

and in the generated class it's like

}
........this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

Now, let us remove the preceding dots.

}
<# } #>
....this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

and in the generated class it's like

}
....this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

OTHER TIPS

I think it's good that you strive for readable generated code. We will sit and try to debug the generated code occassionally so it's good if it's easy on the eyes (ofc we never edit the generated code).

I have adopted a pattern where I might sacrifice some readability of the template to gain generated code readability.

Generated code
<#
    T4 statements
#>
Generated code

IE #> always appear after a newline and a newline is added immedietly after.

Your code then would be changed into:

}
<# 
    } 
#>
    this.cmbDecisionList.Dat = dataSource;
    this.btnDec.Enabled = dataSource.Count > 0;
}

That way the generated code tends to be formatted as intended.

It's probably not the only way to retain the formatting as intended but it's the one I use.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top