protected RequireSettings IncludeResource(string resourceType, string resourcePath, Context context) { var workContext = context.GetWorkContext(); var resourceManager = workContext.Resolve <IResourceManager>(); var pathResolver = workContext.Resolve <ITemplateItemProvidedPathResolver>(); var renderingContext = context.GetTemplateRenderingContext(); // If a template file is being rendered then resources paths can be used as usual from themes; if a // template item is rendered then relative virtual paths should be handled the same way (those reference // flat files). if (renderingContext.TemplateType == Models.TemplateType.TemplateFile || (resourcePath.StartsWith("~") && pathResolver.IsRealVirtualPath(resourcePath))) { var resourceRegister = new ResourceRegister( new DummyWebPage { VirtualPath = renderingContext.TemplatePath }, resourceManager, resourceType); return(resourceRegister.Include(resourcePath)); } else { if (!resourcePath.StartsWith("//") && !resourcePath.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !resourcePath.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { resourcePath = pathResolver.GenerateUrlFromPath(resourcePath); } return(resourceManager.Include(resourceType, resourcePath, resourcePath)); } }
public void RegisterImageSet(string imageSet, string style = "", int size = 16) { // hack to fake the style "alternate" for now so we don't have to change stylesheet names when this is hooked up // todo: (heskew) deal in shapes so we have real alternates var imageSetStylesheet = !string.IsNullOrWhiteSpace(style) ? string.Format("{0}-{1}.css", imageSet, style) : string.Format("{0}.css", imageSet); Style.Include(imageSetStylesheet); }
public void IncludeSkin(ResourceRegister Style, ResourceRegister Script, string settingsName) { var skinPart = GetConfigurationPart(settingsName); var manifest = GetSkinsManifest(); var allowedSkinNames = GetSkinNames(); if (manifest != null && skinPart != null) { var selectedSkin = manifest.Skins .FirstOrDefault(tsd => allowedSkinNames.Contains(tsd.Name) && tsd.Name.Equals(skinPart.SkinName)); // there may be a Default skin configured in the manifest, to be used // when there is nothing selected in the skinPart if (selectedSkin == null && string.IsNullOrWhiteSpace(skinPart.SkinName)) { selectedSkin = manifest.Skins.FirstOrDefault(tsd => tsd.Name.Equals("Default", StringComparison.OrdinalIgnoreCase)); } if (selectedSkin != null) { // add css files to head of page if (selectedSkin.StyleSheets != null) { foreach (var cssName in selectedSkin.StyleSheets) { var debugPath = GetStyleSheet(cssName); var resourcePath = GetStyleSheet(cssName, true); if (string.IsNullOrWhiteSpace(resourcePath)) { resourcePath = debugPath; } if (!string.IsNullOrWhiteSpace(resourcePath)) { Style.Include(debugPath, resourcePath).AtHead(); } } } // add scripts to head of page if (selectedSkin.HeadScripts != null) { foreach (var scriptName in selectedSkin.HeadScripts) { var debugPath = GetScript(scriptName); var resourcePath = GetScript(scriptName, true); if (string.IsNullOrWhiteSpace(resourcePath)) { resourcePath = debugPath; } if (!string.IsNullOrWhiteSpace(resourcePath)) { Script.Include(debugPath, resourcePath).AtHead(); } } } // add scripts to foot of page if (selectedSkin.FootScripts != null) { foreach (var scriptName in selectedSkin.FootScripts) { var debugPath = GetScript(scriptName); var resourcePath = GetScript(scriptName, true); if (string.IsNullOrWhiteSpace(resourcePath)) { resourcePath = debugPath; } if (!string.IsNullOrWhiteSpace(resourcePath)) { Script.Include(debugPath, resourcePath).AtFoot(); } } } } // add variables that are configured in the part var configuredVariables = skinPart.Variables.Where(v => !string.IsNullOrWhiteSpace(v.Value)); if (configuredVariables.Any()) { // create the style to add to the head of the page var sb = new StringBuilder(); sb.AppendLine("<style>"); sb.AppendLine(":root {"); foreach (var variable in configuredVariables) { sb.AppendLine(string.Format("{0}: {1};", variable.Name, variable.Value)); } sb.AppendLine("}"); sb.AppendLine("</style>"); _resourceManager.RegisterHeadScript(sb.ToString()); } } }