Esempio n. 1
0
 private static string InternalRenderNoCache(StringBuilder pageContent, RenderParams parameters)
 {
     ReplaceFileTags(pageContent);
     if (parameters != null)
     {
         ReplaceTags(pageContent, parameters);
     }
     return(pageContent.ToString());
 }
Esempio n. 2
0
 private static string InternalRenderLite(StringBuilder sb, RenderParams parameters)
 {
     if (parameters == null)
     {
         return(sb.ToString());
     }
     ReplaceTags(sb, parameters);
     return(sb.ToString());
 }
Esempio n. 3
0
 /// <summary>
 ///     Renders the ecs file at the given path
 /// </summary>
 /// <param name="filepath">ecs file path</param>
 /// <param name="parameters">Rendering parameter</param>
 /// <returns></returns>
 public string Render(string filepath, RenderParams parameters)
 {
     if (Path.GetExtension(filepath) != ".ecs")
     {
         throw new ArgumentException("Please use .ecs files for rendering");
     }
     if (_cachePages)
     {
         if (!_renderCache.TryGetValue(filepath, out var cached))
         {
             cached = new CachedFile(filepath);
             _renderCache.TryAdd(filepath, cached);
         }
         var sb = new StringBuilder(cached.Get());
         return(InternalRenderLite(sb, parameters));
     }
     return(InternalRenderNoCache(new StringBuilder(File.ReadAllText(filepath)), parameters));
 }
Esempio n. 4
0
        private static void ReplaceTags(StringBuilder sb, RenderParams parameters)
        {
            var doneHashset = new HashSet <string>();
            var pmatches    = NormalTagRegex.Matches(sb.ToString());

            foreach (Match pmatch in pmatches)
            {
                if (doneHashset.Contains(pmatch.Value))
                {
                    continue;
                }
                var tag  = pmatch.Groups[1].Value;
                var data = parameters[tag];
                if (data == null)
                {
                    continue;
                }
                sb.Replace(pmatch.Value, data);
                doneHashset.Add(pmatch.Value);
            }

            pmatches = HtmlTagRegex.Matches(sb.ToString());
            foreach (Match pmatch in pmatches)
            {
                if (doneHashset.Contains(pmatch.Value))
                {
                    continue;
                }
                var m = pmatch.Groups[1].Value;
                var t = parameters[m];
                if (string.IsNullOrEmpty(t))
                {
                    continue;
                }
                sb.Replace(pmatch.Value, HtmlEncoder.Default.Encode(t));
                doneHashset.Add(pmatch.Value);
            }
        }
Esempio n. 5
0
 /// <summary>
 ///     Renders an ecs page file to HTML and sends it
 /// </summary>
 /// <param name="instance">The instance of the response</param>
 /// <param name="pageFilePath">The path of the ecs file to be rendered</param>
 /// <param name="parameters">The parameter collection used when rendering the template</param>
 /// <param name="status">The status code for the response</param>
 public static async Task RenderPage(this Response instance, string pageFilePath, RenderParams parameters, HttpStatusCode status = HttpStatusCode.OK)
 {
     var page = instance.ServerPlugins.Get <EcsPageRenderer>().Render(pageFilePath, parameters);
     await instance.SendString(page, "text/html", status : status);
 }
        /// <summary>
        ///     Renders an ecs page file to HTML and sends it
        /// </summary>
        /// <param name="response">The instance of the response</param>
        /// <param name="pageFilePath">The path of the ecs file to be rendered</param>
        /// <param name="parameters">The parameter collection used when rendering the template</param>
        /// <param name="status">The status code for the response</param>
        public static async Task <HandlerType> RenderPage(this Response response, string pageFilePath, RenderParams parameters, HttpStatusCode status = HttpStatusCode.OK)
        {
            var page = response.Context.Plugins.Get <EcsPageRenderer>().Render(pageFilePath, parameters);

            return(await response.SendString(page, "application/html", status : status));
        }