internal static string Render(DynamicEntity parent, string entityField, string textTemplate) { // do basic checking if (!InlineCbDetector.IsMatch(textTemplate)) return textTemplate; var result = new StringBuilder(); var charProgress = 0; var matches = InlineCbDetector.Matches(textTemplate); if (matches.Count == 0) return textTemplate; // create edit-object which is necessary for context attributes var edit = new InPageEditingHelper(parent.SxcInstance); foreach (Match curMatch in matches) { // Get characters before the first match if (curMatch.Index > charProgress) result.Append(textTemplate.Substring(charProgress, curMatch.Index - charProgress)); charProgress = curMatch.Index + curMatch.Length; // get the infos we need to retrieve the value, get it. var marker = curMatch.Value.Replace("\'", "\""); if (marker.IndexOf("sxc=\"sxc-content-block\"", StringComparison.Ordinal) == 0) continue; var guidMatch = GuidExtractor.Match(marker); var likelyGuid = guidMatch.Groups[1].Value; // check if guid is valid Guid guid; if (!Guid.TryParse(likelyGuid, out guid)) continue; object objFound; DynamicEntity subitem = null; var found = parent.TryGetMember(entityField, out objFound); if (found) { var itms = (IList<DynamicEntity>)objFound; if (itms?.Count > 0) subitem = itms.FirstOrDefault(i => i.EntityGuid == guid); } result.Append(Simple.RenderWithEditContext(parent, subitem, entityField, guid, edit)); } // attach the rest of the text (after the last match) result.Append(textTemplate.Substring(charProgress)); // Ready to finish, but first, ensure repeating if desired var finalResult = result.ToString(); return finalResult; }
internal static string RenderWithEditContext(DynamicEntity parent, DynamicEntity subItem, string cbFieldName, Guid? newGuid = null, IInPageEditingSystem edit = null) { if (edit == null) edit = new InPageEditingHelper(parent.SxcInstance); var attribs = edit.ContextAttributes(parent, field: cbFieldName, newGuid: newGuid); var inner = (subItem == null) ? "": Render(parent.SxcInstance.ContentBlock, subItem.Entity).ToString(); var cbClasses = edit.Enabled ? WrapperSingleItem : ""; return string.Format(WrapperTemplate, new object[] { cbClasses, attribs, inner}); }
internal static string RenderListWithContext(DynamicEntity parent, string fieldName, IInPageEditingSystem edit = null) { object objFound; var innerBuilder = new StringBuilder(); var found = parent.TryGetMember(fieldName, out objFound); if (found) { var itms = objFound as IList<DynamicEntity>; if(itms != null) foreach (var cb in itms) innerBuilder.Append(Render(cb.SxcInstance.ContentBlock, cb.Entity)); } // create edit object if missing...to re-use in the wh if (edit == null) edit = new InPageEditingHelper(parent.SxcInstance); return string.Format(WrapperTemplate, new object[] { edit.Enabled ? WrapperMultiItems : "", edit.ContextAttributes(parent, field: fieldName), innerBuilder }); }