private ITemplate ConfigureTemplate(Type docType, ref ITemplate defaultTemplate, TemplateAttribute attribute) { var template = _fileService.GetTemplates().FirstOrDefault(x => string.Equals(x.Alias, attribute.TemplateAlias, StringComparison.InvariantCultureIgnoreCase)); if (template == null) { template = CreateTemplate(attribute); } if (attribute.IsDefault) { if (defaultTemplate == null) { defaultTemplate = template; } else { throw new CodeFirstException("More than one default template specified for " + docType.FullName); } } if (attribute.TemplateName != template.Name) { var t = new umbraco.cms.businesslogic.template.Template(template.Id); t.Text = attribute.TemplateName; t.Save(); template = _fileService.GetTemplates().FirstOrDefault(x => x.Alias == attribute.TemplateAlias); //re-get the template to pick up the changes } return template; }
private ITemplate CreateTemplate(TemplateAttribute attribute) { var path = "~/Views/" + attribute.TemplateAlias.ToPascalCase() + ".cshtml"; var template = new Template(path, attribute.TemplateName, attribute.TemplateAlias); lock (_lock) { if (System.IO.File.Exists(HostingEnvironment.MapPath(path))) { //get the existing content from the file so it isn't overwritten when we save the template. template.Content = System.IO.File.ReadAllText(HostingEnvironment.MapPath(path)); } else { //TODO get this from a file resource containing a default view var content = "@inherits Felinesoft.UmbracoCodeFirst.Views.UmbracoDocumentViewPage<" + attribute.DecoratedTypeFullName + ">" + Environment.NewLine + Environment.NewLine; template.Content = content; } _fileService.SaveTemplate(template); } return template; }