Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:30:12+00:00 2026-06-04T14:30:12+00:00

I am creating a T4 template that I wish to conditionally include templates based

  • 0

I am creating a T4 template that I wish to conditionally include templates based on some criteria. For example I have tried the following…

<#switch(iocContainer) {#>
<#case "Autofac":#>
    <#@ include file="Autofac\EntityTemplate.ttinclude" #>
    <#@ include file="Autofac\ServiceTemplate.ttinclude" #>
    <#@ include file="Autofac\RepositoryTemplate.ttinclude" #>
    <#@ include file="Autofac\DbContextTemplate.ttinclude" #>
<#break;#>

<#case "Castle":#>
    <#@ include file="Castle\EntityTemplate.ttinclude" #>
    <#@ include file="Castle\ServiceTemplate.ttinclude" #>
    <#@ include file="Castle\RepositoryTemplate.ttinclude" #>
    <#@ include file="Castle\DbContextTemplate.ttinclude" #>
<#break;#>

<#case "nInject":#>
    <#@ include file="nInject\EntityTemplate.ttinclude" #>
    <#@ include file="nInject\ServiceTemplate.ttinclude" #>
    <#@ include file="nInject\RepositoryTemplate.ttinclude" #>
    <#@ include file="nInject\DbContextTemplate.ttinclude" #>
<#break;
}#>

The problem is when the templating engine runs, it appears to preprocess all the includes before evaluating any code. So the switch statement above isn’t running and T4 attempts to include all the files.

Is there a way to conditionally include T4 templates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:30:13+00:00Added an answer on June 4, 2026 at 2:30 pm

    No, T4 doesn’t include conditional includes by design. The directives will all be processed.

    If you invert your logic you can solve this. Split the T4 content above and below your switch statement into two separate files (let’s call them start.ttinclude and end.ttinclude). Then create three separate T4 templates called, say, autofac.tt, castle.tt and ninject.tt. These would each looks something like this (this is castle.tt):

    <#@ include file="start.ttinclude" #>
    <#@ include file="Castle\EntityTemplate.ttinclude" #>
    <#@ include file="Castle\ServiceTemplate.ttinclude" #>
    <#@ include file="Castle\RepositoryTemplate.ttinclude" #>
    <#@ include file="Castle\DbContextTemplate.ttinclude" #>
    <#@ include file="end.ttinclude" #>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know some of the tutorials for creating Xcode project templates, for example this
I'm looking at the ComboBox template . I have a StoryBoard that I'm creating
I could use some help creating an XSL template that will take a string
I am creating my own custom T4 Template that integrates with an ADO.NET Entity
I am creating a Multi project Template The problem is that when I run
I'm looking for some LaTeX template for creating quality output. On R-bloggers I've bumped
Im creating a GSP form that i wish to submit using the $.ajax() call.
I am creating a template for a button. I want the text that is
I'm creating a t4 template that will generate an enum from a number of
After creating a new Silverlight application from the JetPack theme template that was installed,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.