Write() public method

Writes an object value to the current Output.
public Write ( SourceSpan span, object textAsObject ) : void
span Scriban.Parsing.SourceSpan The span of the object to render.
textAsObject object The text as object.
return void
Ejemplo n.º 1
0
        private object EvaluateAndRender(TemplateContext context, bool render)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            CheckErrors();

            // Make sure that we are using the same parserOptions
            if (SourceFilePath != null)
            {
                context.PushSourceFile(SourceFilePath);
            }

            try
            {
                context.UseScientific = LexerOptions.Lang == ScriptLang.Scientific;
                var result = context.Evaluate(Page);
                if (render)
                {
                    if (Page != null && context.EnableOutput && result != null)
                    {
                        context.Write(Page.Span, result);
                    }
                }
                return(result);
            }
            finally
            {
                if (SourceFilePath != null)
                {
                    context.PopSourceFile();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Renders this template using the specified context. See remarks.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <exception cref="System.ArgumentNullException">If context is null</exception>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <remarks>
        /// When using this method, the result of rendering this page is output to <see cref="TemplateContext.Output"/>
        /// </remarks>
        public void Render(TemplateContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (HasErrors) throw new InvalidOperationException("This template has errors. Check the <Messages> property for more details");

            // Make sure that we are using the same options
            if (SourceFilePath != null)
            {
                context.PushSourceFile(SourceFilePath);
            }

            try
            {
                context.Result = null;
                Page?.Evaluate(context);

                if (Page != null && context.EnableOutput && context.Result != null)
                {
                    context.Write(Page.Span, context.Result);
                    context.Result = null;
                }
            }
            finally
            {
                if (SourceFilePath != null)
                {
                    context.PopSourceFile();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Evaluates the template using the specified context. See remarks.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <param name="render"><c>true</c> to render the output to the <see cref="TemplateContext.Output"/></param>
        /// <exception cref="System.ArgumentNullException">If context is null</exception>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        private object EvaluateAndRender(TemplateContext context, bool render)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            CheckErrors();

            // Make sure that we are using the same parserOptions
            if (SourceFilePath != null)
            {
                context.PushSourceFile(SourceFilePath);
            }

            try
            {
                if (Page == null || Page.Body.Statements.Count == 0)
                {
                    return(null);
                }

                if (render)
                {
                    ResolvePageLayout(context);
                }

                var result = context.Evaluate(Page);
                if (render)
                {
                    if (Page != null && context.EnableOutput && result != null)
                    {
                        context.Write(Page.Span, result);
                    }
                }
                return(result);
            }
            finally
            {
                if (SourceFilePath != null)
                {
                    context.PopSourceFile();
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Evaluates the template using the specified context. See remarks.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <exception cref="System.ArgumentNullException">If context is null</exception>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        private object EvaluateAndRender(TemplateContext context, bool render)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (HasErrors)
            {
                throw new InvalidOperationException("This template has errors. Check the <Messages> property for more details");
            }

            // Make sure that we are using the same parserOptions
            if (SourceFilePath != null)
            {
                context.PushSourceFile(SourceFilePath);
            }

            try
            {
                var result = context.Evaluate(Page);
                if (render)
                {
                    if (Page != null && context.EnableOutput && result != null)
                    {
                        context.Write(Page.Span, result);
                    }
                }
                return(result);
            }
            finally
            {
                if (SourceFilePath != null)
                {
                    context.PopSourceFile();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Renders this template using the specified context. See remarks.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <exception cref="System.ArgumentNullException">If context is null</exception>
        /// <exception cref="System.InvalidOperationException">If the template <see cref="HasErrors"/>. Check the <see cref="Messages"/> property for more details</exception>
        /// <remarks>
        /// When using this method, the result of rendering this page is output to <see cref="TemplateContext.Output"/>
        /// </remarks>
        public void Render(TemplateContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (HasErrors)
            {
                throw new InvalidOperationException("This template has errors. Check the <Messages> property for more details");
            }

            // Make sure that we are using the same options
            if (SourceFilePath != null)
            {
                context.PushSourceFile(SourceFilePath);
            }

            try
            {
                context.Result = null;
                Page?.Evaluate(context);

                if (Page != null && context.EnableOutput && context.Result != null)
                {
                    context.Write(Page.Span, context.Result);
                    context.Result = null;
                }
            }
            finally
            {
                if (SourceFilePath != null)
                {
                    context.PopSourceFile();
                }
            }
        }