T4MVC is a helpful template for your MVC projects. You can follow the directions on David’s blog to get started; that should give you a quick understanding of what T4MVC is capable of. If you’ve yet to experience the joy of writing T4 templates, you should also check out Scott Hanselman’s Blog and Oleg Sych’s blog. You know what they say: if you haven’t been to Oleg’s T4 blog, you haven’t really “experienced” T4.
Also, a sidenote: Wayne Brantley has created a nice VS AddIn that allows for T4MVC autosaving. No more dirty, dirty (Christina Aguilera, anyone?) saving. If you’ve never tried using T4MVC without Brantley’s AddIn, consider yourself lucky. You probably haven’t had a monster-laden nightmare about right-clicking and selecting Run Custom Tool. I sure have.
Okay. Once you’ve added both T4MVC.tt and T4MVC.settings.t4 to your budding project, go ahead and rename T4MVC.tt to T4MVC.tt.base:
Right click on your T4MVC folder and create a new file. Name it T4MVC.tt:
Open T4MVC.tt. Add this preprocessor directive to the file:
<#@ Include File="T4MVC.base.t4" #>
That gives you access to all of the nice EnvDTE method madness that David has sorted out for us in his initial T4MVC file. Now, we can use the methods in T4MVC.base.t4 to generate the code that we want for our project. In this case, we’ll add some simple extensions to our HtmlHelper class that will allow for explicit partial rendering:
<#@ Include File="T4MVC.base.t4" #>
#pragma warning disable 1591
#region T4MVC Extension
namespace Big.Fat.HtmlHelpers
{
[GeneratedCode("T4MVC", "2.0")]
public static class ExplicitPartialBuilder
{
<#
var sharedController = GetControllers()
.Where(m => m.SharedViewFolder).First();
var partials = sharedController
.ViewList.Views.Where(m => m.Value.EndsWith(".ascx"));
foreach (var prt in partials) {
//get partial name
var partialName = prt.Key;
var partialPath = prt.Value;
#>
///<summary>
///Render <#=partialName#> Partial.
///Equivalent to Html.RenderPartial("<#=partialName#>");
///</summary>
public static void Render<#=partialName#>(this HtmlHelper html)
{
//render partial
html.RenderPartial("<#=partialPath#>");
}
///<summary>
///Render <#=partialName#> Partial.
///Equivalent to Html.RenderPartial("<#=partialName#>", model);
///</summary>
public static void Render<#=partialName#>(this HtmlHelper html, object model)
{
//render partial
html.RenderPartial("<#=partialPath#>", model);
}
<# } #>
}
}
#endregion
#pragma warning restore 1591
Right click on T4MVC.tt and select Run Custom Tool. A C# file will be generated. You can find it by expanding the T4 template. Here’s the file my project generated. (FYI, I have two lonely partials, “Header” and “Footer” in my Shared/Partials folder). Do a little searching, and you’ll find the code we generated:
#pragma warning disable 1591
#region T4MVC Extension
namespace Big.Fat.HtmlHelpers
{
[GeneratedCode("T4MVC", "2.0")]
public static class ExplicitPartialBuilder
{
///<summary>
///Render Footer Partial.
///Equivalent to Html.RenderPartial("Footer");
///</summary>
public static void RenderFooter(this HtmlHelper html)
{
//render partial
html.RenderPartial("~/Views/Shared/Footer.ascx");
}
///<summary>
///Render Footer Partial.
///Equivalent to Html.RenderPartial("Footer", model);
///</summary>
public static void RenderFooter(this HtmlHelper html, object model)
{
//render partial
html.RenderPartial("~/Views/Shared/Footer.ascx", model);
}
///<summary>
///Render Header Partial.
///Equivalent to Html.RenderPartial("Header");
///</summary>
public static void RenderHeader(this HtmlHelper html)
{
//render partial
html.RenderPartial("~/Views/Shared/Header.ascx");
}
///<summary>
///Render Header Partial.
///Equivalent to Html.RenderPartial("Header", model);
///</summary>
public static void RenderHeader(this HtmlHelper html, object model)
{
//render partial
html.RenderPartial("~/Views/Shared/Header.ascx", model);
}
}
}
#endregion
#pragma warning restore 1591
In my ViewPage, I can now render my partials with their personalized methods:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderHeader(); %>
...
<% Html.RenderFooter(); %>
</asp:Content>
Read More
You can leave a response, or trackback from your own site.
12 Responses to “T4MVC Extension for MVC Partials”
Leave a Reply




That’s cool! But where does GetPartials() come from?
That is very cool.
Hey David,
Whoops. Didn’t realize that I was using that helper method. I often confuse myself as such. I’ve updated the T4 source in the post. Thanks for the help.
BTW–I have a couple more of these extensions that I’ll be posting in the next couple of days–would love to hear your thoughts/concerns/complaints.
Cheers,
Evan
It would be cool to get this extension in the main template if you want to contribute it. It looks nice and simple, and would make a good addition. See http://mvccontrib.codeplex.com/wikipage?title=T4MVC_contrib&referringTitle=T4MVC
Feel free to contact me by email (david.ebbo (at) microsoft.com) to discuss.
Hey David. I’d be happy to submit this, and will be annoying you via e-mail soonly.
T4MVC Extension for MVC Partials – Evan Nagle…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] T4MVC Extension for MVC Partials – Evan Nagle talks through extending the T4MVC projects templates to include support for MVC Partials [...]
[...] start, check out my first blog about T4MVC, here. Our current T4MVC journey (i.e. “Creating explicit ActionLinks”) will be a wee bit [...]
[...] first T4MVC extension we created allows for the explicit rendering of partials. Here’s a quick [...]
Hi Evan,
Thanks for submitting the change! It looks good, except that it doesn’t seem to use the IsHttpPost flag that you added. I assume it was related to some other change that you maybe didn’t make? So I think I’ll leave the part out. Can you email me to discuss?
thanks!
David
[...] can see my posts about Explicit T4MVC helpers here, here, and [...]
One suggestion I would also make is the use of Html.Partial as well, for those with MVC2 as I tend to use that these days, if only because it returns a string and is shorter than RenderPartial!