/// <summary> /// Constructs a new <see cref="FluentStringTemplateConfiguration"/> instance using the supplied <see cref="StringTemplateConfiguration"/> /// </summary> /// <param name="cfg">The configuration</param> public FluentStringTemplateConfiguration(StringTemplateConfiguration cfg) { _cfg = cfg; }
/// <summary> /// Renders a string template using the supplied object /// </summary> /// <param name="template">the template</param> /// <param name="replacements">dictionary of replacement values</param> /// <param name="cfg">override configuration</param> /// <returns></returns> public static string Render(string template, Dictionary <string, object> replacements, StringTemplateConfiguration cfg) => ReplaceText(template, replacements, cfg);
/// <summary> /// Renders a string template using the supplied object /// </summary> /// <param name="template">the template</param> /// <param name="obj">any POCO</param> /// <param name="cfg">override configuration</param> /// <returns></returns> public static string Render(string template, object obj, StringTemplateConfiguration cfg) => ReplaceText(template, BuildPropertyDictionary(obj), cfg);
/// <summary> /// Renders a string template using the supplied object /// </summary> /// <param name="template">the template</param> /// <param name="obj">any POCO</param> /// <param name="replacements">additional dictionary of replacement values</param> /// <param name="cfg">override configuration</param> /// <returns></returns> public static string Render(string template, object obj, Dictionary <string, object> replacements, StringTemplateConfiguration cfg) => ReplaceText(template, BuildPropertyDictionary(obj).Union(replacements).ToDictionary(x => x.Key, x => x.Value), cfg);
/// <summary> /// This performs all of the token replacements and recursion /// </summary> /// <param name="text">The snippet to process for the supplied replacements</param> /// <param name="replacements">The replacements</param> /// <param name="cfg">The configuration</param> /// <returns></returns> internal static string ReplaceText(string text, Dictionary <string, object> replacements, StringTemplateConfiguration cfg) => replacements.ToList().OrderBy((kvp) => (kvp.Value is IEnumerable && kvp.Value.GetType() != typeof(string)) ? 1 : 2).Aggregate(text, (c, k) => (k.Value is IEnumerable enumerable && !(k.Value is string) && c.IndexOf($"{cfg.OpenToken}{cfg.ForeachToken} {k.Key}{cfg.CloseToken}") >= 0 && c.IndexOf($"{cfg.OpenToken}/{cfg.ForeachToken} {k.Key}{cfg.CloseToken}") > 0) ?