0

In our project we've stumbled upon the following problem: we need to provide our developers two kinds of master pages (Razor-based and Webforms-based) each of which will generate exactly the same HTML output.

Our initial thought was to use the T4 templating engine and to have only one template which will spawn 2 files: *.master and *.cshtml where, for instance @using Razor directive will correspond to <%@ Import ... webforms statement.

The key idea is that HTML parts shouldn't be duplicated so obviously we cannot use the approach recommended in Generating more than one output file from a template or How to use T4 to generate two files at the same time from one template? because all the static content (plain HTML) in such a case will be duplicated.

Obviously I can write my custom T4 generator but it will be to sophisticated because there are too much directives (like "<%=" which will correspond to "@" in Razor) on the pages.

Any other ideas on how I can accomplish this using any available templating engine?

Community
  • 1
  • 1
xenn_33
  • 855
  • 1
  • 10
  • 25

1 Answers1

2

After some brainstorming custom templates ( http://www.olegsych.com/2008/09/t4-tutorial-creating-reusable-code-generation-templates/ ) did the trick.

The idea is having two templates: one template for holding the HTML and conditional rendering logic and the other template for running the first one two times with different parameters.

A template-runner may look like the following:

<#@ include file="GenericMasterPageTemplate.tt" #>
<#
GenericMasterPageTemplate genericMasterPageTemplate = new  GenericMasterPageTemplate();
genericMasterPageTemplate._viewEngine = "Razor";
genericMasterPageTemplate.Output.File = @"PATH_TO_OUTPUT_RAZOR_TEMPLATE";
genericMasterPageTemplate.Render();

genericMasterPageTemplate._viewEngine = "Webforms";
genericMasterPageTemplate.Output.File = @"PATH_TO_OUTPUT_WEBFORMS_TEMPLATE";
genericMasterPageTemplate.Render();
#>

Obviously the conditional logic within the first template will analyze the value of _viewEngine parameter and render the necessary directives appropriately.

xenn_33
  • 855
  • 1
  • 10
  • 25
  • If this works for you it sounds ok to me. Possibly you could have one TT file if you put all generation code in a function in the T4 file and call it twice but as I never render a template to more than one file I am not 100% sure if this approach works. – Just another metaprogrammer Oct 11 '11 at 05:14
  • Thank you for the comment. My thought is that having one template which can interpret different input parameters and the other file which will set up and run the template is a good thing in terms of separation of responsibilities. Besides taking into account T4 intrinsics I think that technically it's impossible to unite the reusable template with parameters and code which runs it. Maybe I'm wrong with this particular point but it's a different question though. – xenn_33 Oct 11 '11 at 10:32
  • Without knowing the details on how the template looks I cant say whether its impossible or not but its good that you find a solution you like. – Just another metaprogrammer Oct 11 '11 at 17:19