public void ResolveTemplateSource(TemplateSourceResolveContext context) { if (string.IsNullOrWhiteSpace(context.FileName)) { return; } var fileName = context.FileName; if (!fileName.EndsWith(".cshtml") && fileName.Contains(".cshtml")) { fileName = fileName.Substring(0, fileName.LastIndexOf(".cshtml")); } fileName += ".cshtml"; var uriDir = string.Empty; var templatePath = string.Empty; // I look into the Site folder for Code. uriDir = string.Format("~/App_Data/Sites/{0}/{1}", _shellSettings.Name, "Code"); templatePath = Path.Combine(uriDir, fileName); if (templatePath.StartsWith("~")) { templatePath = HostingEnvironment.MapPath(templatePath); } if (File.Exists(templatePath)) { context.Source = File.ReadAllText(templatePath); context.FileName = fileName; } // Now I need to look into the fallback tenants if (string.IsNullOrWhiteSpace(context.Source)) { foreach (var ft in GetFallbackTenants()) { if (string.IsNullOrWhiteSpace(context.Source)) { uriDir = string.Format("~/App_Data/Sites/{0}/{1}", ft, "Code"); templatePath = Path.Combine(uriDir, fileName); if (templatePath.StartsWith("~")) { templatePath = HostingEnvironment.MapPath(templatePath); } if (File.Exists(templatePath)) { context.Source = File.ReadAllText(templatePath); context.FileName = fileName; break; } } } } }
public ITemplateSource Resolve(ITemplateKey key) { ITemplateSource result; if (_dynamicTemplates.TryGetValue(key, out result)) { return(result); } var ctx = new TemplateSourceResolveContext() { Key = key, Source = string.Empty, FileName = key.Name }; if (!string.IsNullOrWhiteSpace(key.Name)) { // I need to read resolvers from WorkContext here, because if I injected them in the class constructor they wouldn't work properly. // They cache their content throughout multiple requests and fail to update content templates when needed. // This happens because IRazorTemplateManager inherits from ISingletonDependency. var resolvers = _orchardServices.WorkContext .Resolve <IEnumerable <ICustomRazorTemplateResolver> >() .OrderByDescending(r => r.Priority); foreach (var res in resolvers) { if (!string.IsNullOrWhiteSpace(ctx.Source)) { break; } res.ResolveTemplateSource(ctx); } } if (string.IsNullOrWhiteSpace(ctx.Source)) { return(_defaultTemplateManager.Resolve(key)); } else { return(new LoadedTemplateSource(ctx.Source, ctx.FileName)); } }
public void ResolveTemplateSource(TemplateSourceResolveContext context) { if (string.IsNullOrWhiteSpace(context.FileName)) { return; } var fileName = context.FileName; if (context.Key.TemplateType == ResolveType.Include) { fileName = fileName.Substring(0, fileName.LastIndexOf(":Version")); } // I need to search for the key into the CustomTemplates. var customTemplate = GetTemplate(fileName); if (customTemplate != null) { context.Source = customTemplate.Text; fileName += customTemplate.ContentItem.VersionRecord.Id.ToString(); context.FileName = fileName + ".cshtml"; } }