Running T4 templates from other T4 template

后端 未结 4 1453
攒了一身酷
攒了一身酷 2020-12-31 12:41

does anyone know if it\'s possible to run T4 template file from another T4 template, inside VS2010

Thank

4条回答
  •  隐瞒了意图╮
    2020-12-31 13:12

    We do this a lot. Here is an example how we reuse a common T4 template yet "pass parameters" into it:

    <#
    var currentUsername = "billclinton"; // this is for integration tests impersonating a user in our case
    #>
    <#@ include file="..\SomeTemplateThatIWantToReuseHere.tt" #>
    

    And we keep our T4 template "generic" by dynamically determining the location that the T4 template is actually ran in (in this case, the T4 template that has the include line in it):

    string namespaceName = code.VsNamespaceSuggestion();
    var namespaceParts = namespaceName.Split('.');
    var currentNamespaceLeg = namespaceParts.Last();
    

    This allows us to do some very powerful templating without the need to duplicate our templates. The only thing being "duplicated" are our 4-line .tt files that have the include call in them, but these are pretty much maintenance free except whatever "configuration" we want to perform by passing in variables in the way we do it.

提交回复
热议问题