private static void ProcessTemplate(string template) { string tname = ""; string tval; int i = template.IndexOf(SECBEGIN); while (i >= 0) { tval = template.Substring(0, i); if (!string.IsNullOrEmpty(tval)) { Templs[tname] = new Templater(tval); } template = template.Substring(i + SECBEGIN.Length); i = template.IndexOf(SECTAGEND); tname = template.Substring(0, i); template = template.Substring(i + SECTAGEND.Length); i = template.IndexOf(SECBEGIN); } if (!string.IsNullOrEmpty(template)) { Templs[tname] = new Templater(template); } }
public string Process(params object[] data) { StringBuilder sb = new StringBuilder(); int level = 0; int ignore = int.MaxValue; foreach (TemplatePart s in parts) { switch (s.Mode) { case TempaltePartMode.Value: if (level < ignore) { string v = GetValue(s.Key, data)?.ToString(); if (!string.IsNullOrEmpty(v)) { sb.Append(v); } } break; case TempaltePartMode.Literal: if (level < ignore) { sb.Append(s.Key); } break; case TempaltePartMode.Start: level++; if (level < ignore) { object v = GetValue(s.Key, data); if (v == null || (v is string && ((string)v).Length == 0)) { ignore = level; } } break; case TempaltePartMode.End: level--; if (level < ignore) { ignore = int.MaxValue; } break; case TempaltePartMode.Call: if (level < ignore) { string[] cmd = s.Key.Split(':'); if (cmd.Length != 2) { throw new Exception("Template call placeholder format is <value>:<template>"); } object v; if (cmd[0].Length > 0) { v = GetValue(cmd[0], data); if (v == null) { break; } } else { v = data; } Templater templ = WebUI.Templs[cmd[1]]; if (templ == null) { break; } if (v is Array) { foreach (object o in (Array)v) { if (o != null) { sb.Append(templ.Process(o)); } } } else if (v is IList) { foreach (object o in (IList)v) { if (o != null) { sb.Append(templ.Process(o)); } } } else { sb.Append(templ.Process(v)); } } break; } } return(sb.ToString()); }