21
Chris Melinn

Introducing T4 Templates

Embed Size (px)

DESCRIPTION

Presented at DevEvening Melbourne # 1.

Citation preview

Page 1: Introducing T4 Templates

Chris Melinn

Page 2: Introducing T4 Templates

Agenda

This presentation will provide a

short overview of T4. We will learn

the basics of creating a template and

then see a few examples of how T4 is

used by others.

The Basics

In the Wild

More Information

Page 3: Introducing T4 Templates
Page 4: Introducing T4 Templates

What is T4?

T4 =

Text

Template

Transformation

Toolkit

Code Generation

Built by Microsoft

Included in Visual Studio

Page 5: Introducing T4 Templates

What is T4?

Generates any textual format

Common examples:

• Code (C#, VB, etc)

• SQL

• XML

Template Based

Similar syntax to ASP

Very Extensible

Page 6: Introducing T4 Templates

But…

Editing T4 sucks *

* with the default Visual Studio setup

No Syntax Highlighting

No Intellisense

Looks like Notepad…

Page 7: Introducing T4 Templates

The DefaultVisual Studio Editor

Page 8: Introducing T4 Templates

Fortunately, there is a solution…

Free Visual T4 Editor

Community Edition

from Clarius Consulting

Much better experience

Syntax Highlighting

Intellisense in Pro Edition

Page 9: Introducing T4 Templates

The Visual T4 Editor (inside Visual Studio)

Page 10: Introducing T4 Templates

Creating Templates

Page 11: Introducing T4 Templates

How it works

T4 engine performs two steps to generate output from a template:

Source: Oleg Sych http://www.olegsych.com/

Page 12: Introducing T4 Templates

Examples of T4 in the wild

Page 13: Introducing T4 Templates

“If you’re going to do something twice or more, manually, in your company, generate it.”

~ Scott Hanselman

Page 14: Introducing T4 Templates

T4 can be used to generate…

Code Templates

(MVC Add View)

Database to Code

Code to Database

Configuration Files

Domain-Specific Languages

(DSL Toolkit)

Removing

Magic Strings

(T4MVC)

Add logic to CSS

(T4CSS)

Page 15: Introducing T4 Templates

ASP.NET MVC CodeTemplates

Page 16: Introducing T4 Templates

T4CSS

Page 17: Introducing T4 Templates

For more information…

Page 18: Introducing T4 Templates

Resources

Oleg Sych’s blog is the best collection of T4 resources I have found anywhere

http://www.olegsych.com/

His CodePlex project, T4 Toolbox, contains several ready-to-use code generation templates and other supporting extensions.

http://www.codeplex.com/t4toolbox

Page 21: Introducing T4 Templates

Contact Me

Blog: http://chrismelinn.wordpress.com

Twitter: http://twitter.com/ChrisMelinn