コード例 #1
0
        /// <summary>
        /// Compiles and renders a template.
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="context">The <see cref="TemplateContext"/>.</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static IResult CompileTemplate(this TemplateContext context, string name, IReader reader)
        {
            if (reader == null)
            {
                return(new EmptyCompileTemplate());
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            var env = context.Environment;

            if (!context.EnableCache && context.Debug)
            {
                System.Diagnostics.Trace.TraceWarning("WARN:The template cache is disabled.");
                return(env.Compile(name, reader.ReadToEnd(context), (c) => context.CopyTo(c)));
            }

            return(env.Results.GetOrAdd(name, (fullName) =>
            {
                return env.Compile(fullName, reader.ReadToEnd(context), (c) => context.CopyTo(c));
            }));
        }
コード例 #2
0
        /// <summary>
        /// 呈现模板
        /// </summary>
        /// <param name="writer">writer</param>
        /// <param name="context">context</param>
        public virtual void Render(TextWriter writer, TemplateContext context)
        {
            var t = Runtime.Templates[this.TemplateKey];

            if (t == null)
            {
                var text = this.TemplateContent;
                t = Runtime.Templates[this.TemplateKey] = Compiler.Compile(this.TemplateKey, text, (ctx) =>
                {
                    context.CopyTo(ctx);
                });
                if (t == null)
                {
                    throw new Exception.TemplateException($"compile error.");
                }
                this.TemplateContent = null;
            }
            t.Render(writer, context);
        }
コード例 #3
0
        /// <summary>
        /// Compiles and renders a template.
        /// </summary>
        /// <param name="path">The fully qualified path of the file to load.</param>
        /// <param name="context">The <see cref="TemplateContext"/>.</param>
        /// <returns></returns>
        public static IResult CompileFile(this TemplateContext context, string path)
        {
            var full = context.FindFullPath(path);

            if (full == null)
            {
                throw new TemplateException($"\"{ path }\" cannot be found.");
            }
            var template = context.Environment.Results.GetOrAdd(full, (fullName) =>
            {
                var res = context.Load(fullName);
                if (res == null)
                {
                    throw new TemplateException($"Path:\"{path}\", the file could not be found.");
                }
                return(context.Environment.Compile(fullName, res.Content, (c) => context.CopyTo(c)));
            });

            return(template);
        }
コード例 #4
0
        /// <summary>
        /// 编译并执行模板
        /// </summary>
        /// <param name="fileName">模板路径</param>
        /// <param name="ctx">TemplateContext</param>
        /// <returns></returns>
        public static string CompileFileAndExec(string fileName, TemplateContext ctx)
        {
            var full = ctx.FindFullPath(fileName);

            if (full == null)
            {
                throw new Exception.TemplateException($"\"{ fileName }\" cannot be found.");
            }
            var template = Runtime.Templates[full];

            if (template == null)
            {
                template = CompileFile(full, full, (c) => ctx.CopyTo(c));
                if (template == null)
                {
                    throw new Exception.TemplateException($"\"{ fileName }\" compile error.");
                }
            }
            using (var sw = new System.IO.StringWriter())
            {
                template.Render(sw, ctx);
                return(sw.ToString());
            }
        }
コード例 #5
0
        /// <inheritdoc />
        public virtual void Render(TextWriter writer, TemplateContext context)
        {
            var text = this.TemplateContent;
            var t    = context.Options.CompilerResults.GetOrAdd(this.TemplateKey, (key) =>
            {
                return(TemplateCompiler.Compile(key, text, context.Options, (ctx) =>
                {
                    context.CopyTo(ctx);
                }));
            });

            if (t == null)
            {
                throw new Exception.TemplateException($"compile error.");
            }
            try
            {
                t.Render(writer, context);
            }
            catch (System.Exception e)
            {
                context.AddError(e);
            }
        }
コード例 #6
0
        /// <summary>
        /// Compiles and renders a template.
        /// </summary>
        /// <param name="path">The fully qualified path of the file to load.</param>
        /// <param name="context">The <see cref="TemplateContext"/>.</param>
        /// <returns></returns>
        public static string CompileFileAndExec(this TemplateContext context, string path)
        {
            var full = context.FindFullPath(path);

            if (full == null)
            {
                throw new Exception.TemplateException($"\"{ path }\" cannot be found.");
            }
            var template = context.Options.CompilerResults.GetOrAdd(full, (name) =>
            {
                var res = context.Options.Loader.Load(path, context.Options.Encoding, context.Options.ResourceDirectories.ToArray());
                if (res == null)
                {
                    throw new Exception.TemplateException($"Path:\"{path}\", the file could not be found.");
                }

                if (string.IsNullOrEmpty(name))
                {
                    name = res.FullPath;
                }
                return(TemplateCompiler.Compile(name, res.Content, context.Options, (c) => context.CopyTo(c)));
            });

            using (var sw = new System.IO.StringWriter())
            {
                template.Render(sw, context);
                return(sw.ToString());
            }
        }