TitanFx.AspNetCore.Mvc.Templating
1.0.0-alpha-1
dotnet add package TitanFx.AspNetCore.Mvc.Templating --version 1.0.0-alpha-1
NuGet\Install-Package TitanFx.AspNetCore.Mvc.Templating -Version 1.0.0-alpha-1
<PackageReference Include="TitanFx.AspNetCore.Mvc.Templating" Version="1.0.0-alpha-1" />
paket add TitanFx.AspNetCore.Mvc.Templating --version 1.0.0-alpha-1
#r "nuget: TitanFx.AspNetCore.Mvc.Templating, 1.0.0-alpha-1"
// Install TitanFx.AspNetCore.Mvc.Templating as a Cake Addin
#addin nuget:?package=TitanFx.AspNetCore.Mvc.Templating&version=1.0.0-alpha-1&prerelease
// Install TitanFx.AspNetCore.Mvc.Templating as a Cake Tool
#tool nuget:?package=TitanFx.AspNetCore.Mvc.Templating&version=1.0.0-alpha-1&prerelease
TitanFx ASP.NET Core MVC Templating
TitanFx ASP.NET Core MVC Templating is an extension library for ASP.NET Core designed to make the creation of templates easier. Rather than only supporting display and editor templates through the use of a view, as is the case for ASP.NET Core, this project allows templates to be injected during the setup of MVC through the use of the ITemplate
interface.
Get Started
To set up your project to use the custom templating system, during the ConfigureServices
method of your Startup
class, you will need to add the following line
services.AddMvcTemplating();
If you wish to preserve the default templates and type discovery rules, use
services.AddMvcTemplating(legacyMode: true);
services.AddDefaultMvcTemplates();
After this, you may add any amount of custom templates to your application as follows
services.AddMvcTemplate<MyCustomTemplate>(TemplatePurposes.Edit, "MyCustomModel");
Creating custom templates
To create a custom template you need to implement the ITemplate
interface. For the majority of cases however, the simpliest approach will be to inherit from CodeTemplate
as this provides a much more useful IHtmlHelper
instance to work with.
class MyCustomTemplate : CodeTemplate
{
protected override IHtmlContent Render(ITemplateService templateService, IHtmlHelper htmlHelper)
{
if (htmlHelper.Model is MyCustomModel model)
{
var builder = new TagBuilder("div");
builder.InnerHtml.SetContent(model.Name);
return builder;
}
return HtmlString.Empty;
}
}
Template naming
When deciding which template to use, the Templating Service will need to compute a list of possible template names. It does this by browsing the interfaces and inheritance of the type of the model being used. For legacyMode: true
this is done with the same rules as MVC out of the box. The default method, however, is more exhaustive and is split into 2 sections: type discovery and type name generation, each is described below.
For each template name produced by either system, a view is first looked for in the folder corresponding to the given purpose with the name generated. If none is found, then a template is searched for in the list of loaded implementations of ITemplate
. If no templates are found either,
Type discovery
- Browse the type stack (
ModelType
,ModelType.BaseType
,ModelType.BaseType.BaseType
, ....) excludingValueType
andObject
. - Browse the interface list, ranked by complexity and then alphabetical, with most complex first. Complexity is the number of interfaces an interface implements. E.g. complexity of
IEnumerable
is 0, but complexity ofIDictionary
is 2 (ICollection
andIEnumerable
). - Finally yield
ValueType
if appropriate, andObject
.
Type naming
For each type discovered above, if the type is not a generic type, simply yield type.Name
. For generic types, first yield a recursively generated name, and then the name of the type with the generic arguments.
Both names for generics are of the form {typeName}({parameterNames})
with parameterNames
being a comma delimited list of the names for each generic parameter of the type.
For example:
int
becomes["Int32"]
Guid
becomes["Guid"]
Dictionary<int, string>
becomes["Dictionary(Int32,String)","Dictionary(TKey,TValue)"]
Dictionary<object, Tuple<long, Task<string>>
becomes["Dictionary(Object,Tuple(Int64,Task(String)))","Dictionary(TKey,TValue)"]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Mvc.ViewFeatures (>= 2.2.0)
- System.Interactive (>= 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-alpha-1 | 2,166 | 11/3/2019 |