public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null) { var componentMatches = regEx.Matches(inputContent); var viewComponentManager = DependencyResolver.Resolve <IViewComponentManager>(); var componentIndexOnPage = 0; if (componentMatches.Count == 0) { foreach (var meta in readFile.GetMeta(nameof(ComponentExpander))) { var componentName = meta.Key; var valueAsArray = (object[])meta.Value; //extract values componentIndexOnPage = (int)valueAsArray[0]; var keyValuePairs = (Dictionary <string, string>)valueAsArray[1]; var componentParameters = GetComponentParameters(keyValuePairs, parameters); viewComponentManager.InvokeViewComponent(componentName, componentParameters, out string _, out object model, out string _, true); MergeModel(parameters, model, componentName, componentIndexOnPage, out string _, out string _); } return(inputContent); } foreach (Match componentMatch in componentMatches) { ExtractMatch(componentMatch, out string[] straightParameters, out Dictionary <string, string> keyValuePairs); if (!straightParameters.Any()) { throw new Exception($"A component must be specified with component tag in view {readFile.FileName}"); } var componentName = straightParameters[0]; //collect the values that are being passed to the component var componentParameters = GetComponentParameters(keyValuePairs, parameters); viewComponentManager.InvokeViewComponent(componentName, componentParameters, out string componentContent, out object model, out string viewPath); if (!viewPath.IsNullEmptyOrWhiteSpace()) { readFile.AddChild(ReadFile.From(viewPath)); } //merge models MergeModel(parameters, model, componentName, componentIndexOnPage, out var assignString, out var resetAssignString); if (!WebHelper.IsAjaxRequest(ApplicationEngine.CurrentHttpContext.Request)) { //add keyvaluepairs as meta along with the index readFile.AddMeta(componentName, new object[] { componentIndexOnPage, keyValuePairs }, nameof(ComponentExpander)); } var match = componentMatch.Result("$0"); //replace only first occurance of the pattern result readFile.Content = readFile.Content.ReplaceFirstOccurance(match, assignString + componentContent + resetAssignString); inputContent = inputContent.ReplaceFirstOccurance(match, assignString + componentContent + resetAssignString); //next component componentIndexOnPage++; } return(inputContent); }
public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null) { var matches = regEx.Matches(inputContent); if (!matches.Any()) { return(inputContent); } var viewAccountant = DependencyResolver.Resolve <IViewAccountant>(); foreach (Match match in matches) { ExtractMatch(match, out string[] straightParameters, out Dictionary <string, string> keyValuePairs); if (!straightParameters.Any()) { throw new Exception($"No control type specified in the tag in view file {readFile.FileName}"); } var controlType = straightParameters[0]; var viewName = viewAccountant.GetThemeViewPath($"Controls/{controlType}"); if (viewName.IsNullEmptyOrWhiteSpace()) { throw new Exception($"Can't find the view {controlType} in view file {readFile.FileName}"); } var controlFile = ReadFile.From(viewName); readFile.AddChild(controlFile); keyValuePairs = keyValuePairs ?? new Dictionary <string, string>(); var keyValuePairsString = keyValuePairs.Where(x => !NonAttributeNames.Contains(x.Key)) .Select(x => $"{x.Key}=\"{x.Value}\"").ToList(); var attributeString = string.Join(" ", keyValuePairsString); var assigns = string.Join("", keyValuePairsString.Select(x => string.Format(AssignFormat, x))); //replace attribute string var controlText = controlFile.Content.Replace("%attributes%", attributeString); foreach (var kp in keyValuePairs.Where(x => NonAttributeNames.Contains(x.Key) || x.Key.StartsWith("__"))) { var key = kp.Key.StartsWith("__") ? kp.Key.Substring("__".Length) : kp.Key; controlText = controlText.Replace($"%{key}%", kp.Value); } //replace the non attributes which were not passed foreach (var nan in NonAttributeNames) { controlText = controlText.Replace($"%{nan}%", ""); } var resetAssigns = string.Join("", keyValuePairs.Select(x => string.Format(AssignFormat, $"{x.Key}=\"\"")).ToList()); readFile.Content = readFile.Content.Replace(match.Result("$0"), assigns + controlText + resetAssigns); inputContent = inputContent.Replace(match.Result("$0"), assigns + controlText + resetAssigns); } return(inputContent); }
public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null) { var widgetMatches = regEx.Matches(inputContent); if (widgetMatches.Count == 0) { return(inputContent); } var pluginSettings = DependencyResolver.Resolve <PluginSettings>(); var widgets = pluginSettings.GetSiteWidgets(true); var viewAccountant = DependencyResolver.Resolve <IViewAccountant>(); var widgetFormat = "{0}"; //do we have a wrapper? var widgetPath = viewAccountant.GetThemeViewPath("Widgets/Index"); if (!widgetPath.IsNullEmptyOrWhiteSpace()) { var widgetFile = ReadFile.From(widgetPath); widgetFormat = widgetFile.Content; } foreach (Match widgetMatch in widgetMatches) { ExtractMatch(widgetMatch, out string[] straightParameters, out Dictionary <string, string> keyValuePairs); if (!straightParameters.Any()) { throw new Exception($"A widget must be specified with zone name in view {readFile.FileName}"); } var zoneName = straightParameters[0]; var widgetBuilder = new StringBuilder(); var paramBuilder = new StringBuilder(); keyValuePairs = keyValuePairs ?? new Dictionary <string, string>(); foreach (var kp in keyValuePairs) { paramBuilder.Append($"{kp.Key}=\"{kp.Value}\" "); } foreach (var widget in widgets.Where(x => x.ZoneName == zoneName)) { var idStr = $"id=\"{widget.Id}\""; var componentStr = $"{{% {string.Format(ComponentFormat, widget.WidgetSystemName, paramBuilder)} {idStr} %}}"; var widgetStr = string.Format(widgetFormat, componentStr); widgetBuilder.Append(widgetStr); widgetBuilder.AppendLine(); } inputContent = inputContent.Replace(widgetMatch.Result("$0"), widgetBuilder.ToString()); readFile.Content = readFile.Content.Replace(widgetMatch.Result("$0"), widgetBuilder.ToString()); } return(inputContent); }
public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null) { var partialMatches = regEx.Matches(inputContent); if (partialMatches.Count == 0) { return(inputContent); } foreach (Match partialMatch in partialMatches) { ExtractMatch(partialMatch, out string[] straightParameters, out Dictionary <string, string> keyValuePairs); if (!straightParameters.Any()) { throw new Exception($"A partial view must be specified with partial tag in view {readFile.FileName}"); } var partialFile = straightParameters[0]; var includeAll = false; if (keyValuePairs != null) { keyValuePairs.TryGetValue("includeAll", out var iaStr); includeAll = iaStr == "true"; } var viewAccountant = DependencyResolver.Resolve <IViewAccountant>(); Func <string, string> partialReplaceAction = s => { var viewFile = ReadFile.From(s); readFile.AddChild(viewFile); //expand the view file var partialViewExpanded = Expand(viewFile, regEx, viewFile.Content); if (viewFile.Content == partialViewExpanded) { var assignString = ""; var resetAssignString = ""; if (keyValuePairs != null) { //create assigns assignString = string.Join("", keyValuePairs.Select(x => string.Format(AssignFormat, x.Key, x.Value))); resetAssignString = string.Join("", keyValuePairs.Select(x => string.Format(AssignFormat, x.Key, ""))); } return(assignString + partialViewExpanded + resetAssignString); } return(""); }; var partialExpanded = ""; if (includeAll) { var allFiles = viewAccountant.GetAllMatchingViewPaths(partialFile); foreach (var file in allFiles) { partialExpanded = partialExpanded + partialReplaceAction(file); } } else { //read the layout now var viewPath = viewAccountant.GetThemeViewPath(partialFile); if (viewPath.IsNullEmptyOrWhiteSpace()) { throw new Exception($"Can't find partial view {partialFile} in view {readFile.FileName}"); } partialExpanded = partialReplaceAction(viewPath); } inputContent = inputContent.Replace(partialMatch.Result("$0"), partialExpanded); readFile.Content = readFile.Content.Replace(partialMatch.Result("$0"), partialExpanded); } return(inputContent); }
public static void ExpandView(string viewName, object parameters, out string expandedContent) { var readFile = ReadFile.From(viewName); expandedContent = SafeExpandView(readFile, readFile.Content, parameters, true); }
public override string Expand(ReadFile readFile, Regex regEx, string inputContent, object parameters = null) { var viewAccountant = DependencyResolver.Resolve <IViewAccountant>(); var layoutMatches = regEx.Matches(inputContent); if (layoutMatches.Count == 0) { if (WebHelper.IsAjaxRequest(ApplicationEngine.CurrentHttpContext.Request)) { //return content without layout in case of raw request var layoutLessContent = readFile.GetMeta(nameof(LayoutExpander)) .FirstOrDefault(x => x.Key == ContentWithoutLayoutKey) .Value?.ToString() ?? inputContent; //is there any ajax layout var ajaxLayoutPath = viewAccountant.GetLayoutPath("_LayoutAjax"); if (!ajaxLayoutPath.IsNullEmptyOrWhiteSpace()) { var ajaxLayout = ReadFile.From(ajaxLayoutPath); return(ajaxLayout.Content.Replace("{% bodyContent %}", layoutLessContent)); } return(layoutLessContent); } return(inputContent); } if (layoutMatches.Count > 1) { throw new Exception($"Can't use two layouts in one page"); } ExtractMatch(layoutMatches[0], out string[] straightParameters, out Dictionary <string, string> keyValuePairs); if (!straightParameters.Any()) { throw new Exception($"A layout must be specified with layout tag in view {readFile.FileName}"); } if (keyValuePairs != null && keyValuePairs.Any(x => x.Key.Equals("ignoreForAjax") && x.Value == "true")) { //preserve content without layout readFile.AddMeta(ContentWithoutLayoutKey, regEx.Replace(readFile.Content, ""), nameof(LayoutExpander)); if (WebHelper.IsAjaxRequest(ApplicationEngine.CurrentHttpContext.Request)) { //return content without layout in case of raw request return(readFile.GetMeta(nameof(LayoutExpander)) .FirstOrDefault(x => x.Key == ContentWithoutLayoutKey) .Value.ToString()); } } var layoutValue = straightParameters[0]; //read the layout now var layoutPath = viewAccountant.GetLayoutPath(layoutValue); if (layoutPath.IsNullEmptyOrWhiteSpace()) { throw new Exception($"Can't find layout {layoutValue} in view {readFile.FileName}"); } var layoutFile = ReadFile.From(layoutPath); readFile.AddChild(layoutFile); //expand the layout file var layoutExpanded = Expand(layoutFile, regEx, layoutFile.Content); if (layoutFile.Content == layoutExpanded) { var bodyMatcher = new Regex(@"{%\s+bodyContent\s+%}"); //remove the layout tag readFile.Content = regEx.Replace(readFile.Content, ""); //remove the body content tag readFile.Content = bodyMatcher.Replace(layoutExpanded, readFile.Content); return(readFile.Content); } return(string.Empty); }