/// <inheritdoc /> public virtual void Render(TextWriter writer, TemplateContext context) { if (templateResult == null) { if (this.Context.Mode == EngineMode.Interpreted) { templateResult = context.InterpretTemplate(this.TemplateKey, this.reader); } else { templateResult = context.CompileTemplate(this.TemplateKey, this.reader); } if (templateResult == null) { throw new TemplateException($"template error."); } } try { templateResult.Render(writer, context); } catch (System.Exception e) { context.AddError(e); } }
/// <summary> /// Throw exception. /// </summary> /// <param name="ctx"></param> /// <param name="e">Represents errors that occur during application execution.</param> /// <param name="tag">Represents errors tag.</param> /// <param name="writer">See the <see cref="System.IO.TextWriter"/>.</param> public static void ThrowException(this TemplateContext ctx, TemplateException e, ITag tag, System.IO.TextWriter writer) { ctx.AddError(e); if (ctx.ThrowExceptions) { writer.Write(tag.ToSource()); } }
/// <summary> /// Throw exception. /// </summary> /// <param name="ctx"></param> /// <param name="e">Represents errors that occur during application execution.</param> /// <param name="tag">Represents errors tag.</param> /// <param name="writer">See the <see cref="System.IO.TextWriter"/>.</param> public static async Task ThrowExceptionAsync(this TemplateContext ctx, TemplateException e, ITag tag, System.IO.TextWriter writer) { ctx.AddError(e); if (!ctx.ThrowExceptions) { await writer.WriteAsync(tag.ToSource()); } }
/// <inheritdoc /> public Task RenderAsync(TextWriter writer, TemplateContext context, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); var t = context.CompileTemplate(this.TemplateKey, this.reader); if (t == null) { throw new TemplateException($"template error."); } try { return(t.RenderAsync(writer, context, cancellationToken)); } catch (System.Exception e) { context.AddError(e); return(Task.FromException(e)); } }
/// <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); } }