Matt Mitchell
Matt Mitchell

Reputation: 41823

Organising T4 Templates

I have begun using T4 Templates for a few DAL enhancements.

However, I find they quickly get unwieldy and often produce poorly formatted code.

I am wondering if anyone has any good tips on organising these templates, and on getting great formatted code at the end.

So far my experience is:

  1. Stick to what existing published templates do (e.g. SubSonic)
  2. If indentation is playing up attempt a blank line after the closest #>
  3. Lots of comments.

Upvotes: 2

Views: 1313

Answers (3)

Jason Robertson
Jason Robertson

Reputation: 149

Use a third party formatting tool to Format your code after generation like Teleriks Just Code

Upvotes: 1

GarethJ
GarethJ

Reputation: 6606

Split your templates into control/data preparation logic and 'views' that do the output rendering.

Set up the data you need first and then write the output with the most minimal logic possible.

If you can parcel up the output into reusable chunks, then you can put those in method wrappers in <#+ #> blocks and do the structural work as a set of method calls.

Upvotes: 2

Robert Koritnik
Robert Koritnik

Reputation: 104999

T4 end result indentation

T4 templates will format code as defined in the template. Indents are as defined in the file itself. To have great indented end result, T4 templates will often look strange and odd indented. But code is coloured so template's code is usually just fine.

T4 organization

I usually have a solution folder with all reusable T4 templates (if lots of them they can be put in subfolders as well). I give them the ttinclude file extension. Others just include these and are put in place where needed.

Upvotes: 1

Related Questions