public static string RenderTemplate(string template, dynamic model, NancyModule page) { if (string.IsNullOrEmpty(template)) { return(""); } template = NancyContextKey.renderKey(template, page.Context); if (template.Contains("@Cookie.")) { template = renderCookie(template, page); } template = renderSite_check(template, page); if (template.Contains("@Module") || template.Contains("@module")) { template = renderModuleView(template, model, page); } var context = page.Context; ViewLocationContext GetViewLocationContext = new ViewLocationContext { Context = context, ModuleName = context.NegotiationContext.ModuleName, ModulePath = context.NegotiationContext.ModulePath }; IRenderContext renderContext = page.ViewFactory.getRenderContextFactory().GetRenderContext(GetViewLocationContext); string data = dx.Render(template, model, new NancyViewEngineHost(renderContext)); data = data.Replace("[ERR!]", string.Empty); if (data.Contains("@modkey") || data.Contains("___modkey")) { data = data.Replace("@modkey", "_modkey").Replace("___modkey", "_modkey"); } if (data.Contains("@pagekey")) { string path = page.Request.Url.Path.Split('?')[0].Split('#')[0]; if (path.Length > 0) { path = path.Substring(1); } path = path.Replace('-', '_'); if (path.EndsWith(hostServer.pathExtSite)) { path = path.Substring(0, path.Length - 4); } data = data.Replace("@pagekey", path); } //return data.renderHashCode(page); return(data); }
//public binJsCssNancy(IConfigProvider configProvider, IJwtWrapper jwtWrapper) public binJsCssNancy() : base("/bin") { //Get["/{folder}/{module}/{theme_key}/{device_type}/css"] = parameters => Get["{cache_id}.css"] = parameters => { string data = ""; string cache_id = "bin/" + parameters.cache_id + ".css"; data = hostServer.getCache(cache_id); data = NancyContextKey.renderKey(data, this.Context); var o = (Response)data; o.StatusCode = Nancy.HttpStatusCode.OK; o.ContentType = "text/css"; return(o); }; Get["{cache_id}.js"] = parameters => { string data = ""; string cache_id = "bin/" + parameters.cache_id + ".js"; data = hostServer.getCache(cache_id); data = NancyContextKey.renderKey(data, this.Context); if (data.Contains("@pagekey")) { string s = this.Request.Headers.Referrer, pagekey = ""; if (!string.IsNullOrEmpty(s)) { s = System.Web.HttpUtility.UrlDecode(s); var a = s.Split('?')[0].Split('#')[0].Split('/'); pagekey = a[a.Length - 1].Replace('-', '_').Replace('.', '_'); if (pagekey == "") { pagekey = a[2].Replace('-', '_').Replace('.', '_'); } } data = data.Replace("@pagekey", pagekey); } var o = (Response)data; o.StatusCode = Nancy.HttpStatusCode.OK; o.ContentType = "text/javascript"; return(o); }; }
private static string renderModuleView(string template, dynamic model, NancyModule page) { string htm = template; foreach (Match m in regEx_IncludeModule.Matches(template)) { string tag = m.ToString(); string modView = m.Groups["ViewName"].Value.ToLower(); string modTemp = "", mod_key = modView + "/" + page.Context.lang_key; if (modView.Contains("api/")) { mod_key = modView; } if (hostModule.dicModule.ContainsKey(mod_key)) { modTemp = hostModule.dicModule[mod_key]; modTemp = NancyContextKey.renderKey(modTemp, page.Context); if (modTemp.Contains("@Cookie.")) { modTemp = renderCookie(modTemp, page); } var partialModel = model; var properties = GetCaptureGroupValues(m, "ParameterName"); if (m.Groups["Model"].Length > 0) { var modelValue = GetPropertyValueFromParameterCollection(partialModel, properties); if (modelValue.Item1 != true) { return(""); // "[ERR!]"; } partialModel = modelValue.Item2; } modTemp = renderModuleView(modTemp, partialModel, page); } htm = htm.Replace(tag, modTemp); } return(htm); }
private static string renderSite_check(string html, NancyModule page) { foreach (Match m in regEx_Command_Check.Matches(html)) { string tag = m.ToString().ToLower(); string code = m.Groups["command_name"].Value.ToLower(); string val_1 = m.Groups["value_check"].Value.ToLower(); string context_key = m.Groups["context_key"].Value.ToLower(); string val_0 = NancyContextKey.getValue(context_key, page.Context); bool ok_ = false; #region int int_0 = 0, int_1 = 0; switch (code) { case "in": val_1 = "," + val_1 + ","; if (val_1.Contains("," + val_0 + ",")) { ok_ = true; } break; case "contain": if (val_1.Contains(val_0)) { ok_ = true; } break; case "null_empty": if (string.IsNullOrEmpty(val_0)) { ok_ = true; } break; case "equal_not": if (val_0 != val_1) { ok_ = true; } break; case "equal": if (val_0 == val_1) { ok_ = true; } break; case "start_with": if (val_0.StartsWith(val_1)) { ok_ = true; } break; case "end_with": if (val_0.EndsWith(val_1)) { ok_ = true; } break; case "greater": int.TryParse(val_0.Trim(), out int_0); int.TryParse(val_1.Trim(), out int_1); if (int_0 > int_1) { ok_ = true; } break; case "less": int.TryParse(val_0.Trim(), out int_0); int.TryParse(val_1.Trim(), out int_1); if (int_0 < int_1) { ok_ = true; } break; case "greater_equal": int.TryParse(val_0.Trim(), out int_0); int.TryParse(val_1.Trim(), out int_1); if (int_0 >= int_1) { ok_ = true; } break; case "less_equal": int.TryParse(val_0.Trim(), out int_0); int.TryParse(val_1.Trim(), out int_1); if (int_0 <= int_1) { ok_ = true; } break; case "between": int.TryParse(val_0.Trim(), out int_0); string[] a = val_1.Split(',').Select(x => x.Trim()).ToArray(); if (a.Length > 1) { int int_2 = 0; int.TryParse(a[0].Trim(), out int_1); int.TryParse(a[1].Trim(), out int_2); if (int_0 <= int_2 && int_0 >= int_1) { ok_ = true; } } break; } #endregion if (ok_) { #region int pos = html.ToLower().IndexOf(tag); while (pos != -1) { string s0 = html.Substring(0, pos); pos = pos + tag.Length; html = html.Substring(pos, html.Length - pos); int end_ = html.ToLower().IndexOf("</" + code + ">"); if (end_ == -1) { pos = -1; html = s0 + " " + html; } else { string content = html.Substring(0, end_); end_ = end_ + code.Length + 3; html = s0 + " " + content + " " + html.Substring(end_, html.Length - end_); pos = html.ToLower().IndexOf(tag); } } #endregion } else { #region int pos = html.ToLower().IndexOf(tag); while (pos != -1) { string s0 = html.Substring(0, pos); pos = pos + tag.Length; html = html.Substring(pos, html.Length - pos); int end_ = html.ToLower().IndexOf("</" + code + ">"); if (end_ == -1) { pos = -1; html = s0 + " " + html; } else { end_ = end_ + code.Length + 3; html = s0 + " " + html.Substring(end_, html.Length - end_); pos = html.ToLower().IndexOf(tag); } } #endregion } } return(html); }