/// <summary> /// Initializes a new instance of the <see cref="Context"/> class. /// </summary> /// <param name="view">The data view to create the context with</param> /// <param name="registry">A reference to the a registry instance</param> /// <param name="parentContext">The parent context for the new context</param> /// <param name="settings">The render settings </param> public Context(object view, Registry registry, Context parentContext, RenderSettings settings) { this.view = view; View = this.view; ParentContext = parentContext; Registry = registry; RenderSettings = settings; Cache = new Dictionary<string, object>() { { ".", TryEnumerationConversionIfRequired(this.view) } }; }
/// <summary> /// Renders a list of tokens with the context, partials and passed original template /// </summary> /// <param name="tokens">The tokens to render</param> /// <param name="context">The context to use when rendering</param> /// <param name="partials">The partials the are available</param> /// <param name="originalTemplate">The original template</param> /// <returns>The rendered tokens as a string</returns> internal string RenderTokens(IList<ParserOutput> tokens, Context context, IDictionary<string, string> partials, string originalTemplate) { CurrentDepth++; if (CurrentDepth >= Registry.MaxRecursionDepth) { throw new StubbleException( $"You have reached the maximum recursion limit of {Registry.MaxRecursionDepth}."); } var sb = new StringBuilder(); foreach (var token in tokens) { var renderable = token as IRenderableToken; if (renderable == null) { continue; } var renderResult = renderable.Render(this, context, partials, originalTemplate); sb.Append(renderResult); } return sb.ToString(); }
/// <summary> /// Takes a template, context, partials and tags and renders the template /// </summary> /// <param name="template">The template to parse and render</param> /// <param name="context">The context object to use to render</param> /// <param name="partials">The partials available to the template</param> /// <param name="tags">The tags to initalise the parser with</param> /// <returns>The template rendered with the passed context object</returns> public string Render(string template, Context context, IDictionary<string, string> partials, Tags tags) { var tokens = Parse(template, tags); var renderResult = RenderTokens(tokens, context, partials, template); ResetCurrentDepth(); return renderResult; }
/// <summary> /// Takes a template, context and partials and Renders them using default tags /// </summary> /// <param name="template">The template to parse and render</param> /// <param name="context">The context object to use</param> /// <param name="partials">The partials available to the template</param> /// <returns>The template rendered with the passed context object</returns> public string Render(string template, Context context, IDictionary<string, string> partials) => Render(template, context, partials, null);