Skip to content

Instantly share code, notes, and snippets.

@thinkfreshnick
Created June 25, 2021 11:12
Show Gist options
  • Save thinkfreshnick/8dd60b760e53e99dca23762fd8fa3516 to your computer and use it in GitHub Desktop.
Save thinkfreshnick/8dd60b760e53e99dca23762fd8fa3516 to your computer and use it in GitHub Desktop.
T4 Sitecore Example
<#@ template language="C#" debug="true" #>
<#@ import namespace="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models" #>
<#@ parameter name="Model" type="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models.SitecoreItem" #>
<#
if (Model is SitecoreTemplate)
{
var template = Model as SitecoreTemplate;
#>
namespace <#= Model.Namespace #>
{
public partial class <#= TitleCase(Model.Name) #> : CustomItem
{
public <#= className #>(Item innerItem) : base(innerItem) { }
<# foreach (var field in template.Fields) { #>
public <#= ReturnType(field.Type) #> <#= TitleCase(field.Name) #>
{
get { return this["<#= field.Name #>"]; }
}
<#}#>
}
}
<#}#>
<#+
public string ReturnType(string typeName)
{
if (typeName == "integer" || typeName == "number")
{
return "int?";
}
return "string";
}
public string TitleCase(string name)
{
name = Regex.Replace(name, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ");
name = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(name);
name = Regex.Replace(name, @"[^a-zA-Z0-9]", String.Empty);
name = Regex.Replace(name, @"(^[0-9])", "Z$1");
return name;
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment