Пример #1
0
        /// <summary>
        /// Render the provided template.
        /// </summary>
        /// <param name="inputTemplate"></param>
        /// <returns>
        /// The rendered output of the template.
        /// If the template is invalid, returns an error message or an empty string according to the current ExceptionHandlingStrategy setting.
        /// </returns>
        /// <remarks>This render method is compatible with the legacy DotLiquid implementation.</remarks>
        public static LavaRenderResult RenderTemplate(string inputTemplate)
        {
            LavaRenderResult result;

            if (_engine == null && _rockLiquidIsEnabled)
            {
                // If a Lava engine is not initialized, fall back to legacy DotLiquid.
                result = new LavaRenderResult();

                result.Text = inputTemplate.ResolveMergeFields(new Dictionary <string, object>());

                return(result);
            }

            result = RenderTemplate(inputTemplate, LavaRenderParameters.Default);

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Render the provided template in a new context with the specified merge fields.
        /// </summary>
        /// <param name="inputTemplate"></param>
        /// <param name="mergeFields">The collection of merge fields to be added to the context used to render the template.</param>
        /// <returns>
        /// The rendered output of the template.
        /// If the template is invalid, returns an error message or an empty string according to the current ExceptionHandlingStrategy setting.
        /// </returns>
        /// <remarks>This render method is compatible with the legacy DotLiquid implementation.</remarks>
        public static LavaRenderResult RenderTemplate(string inputTemplate, IDictionary <string, object> mergeFields)
        {
            LavaRenderResult result;

            if (_engine == null && _rockLiquidIsEnabled)
            {
                // If a Lava engine is not initialized, fall back to legacy DotLiquid.
                // This allows developers to use a single method for basic template rendering that is backward-compatible with the RockLiquid implementation.
                result = new LavaRenderResult();

                result.Text = inputTemplate.ResolveMergeFields(mergeFields);

                return(result);
            }

            result = RenderTemplate(inputTemplate, LavaRenderParameters.WithContext(_engine.NewRenderContext(mergeFields)));

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Render the provided template using the specified parameters.
        /// </summary>
        /// <param name="inputTemplate"></param>
        /// <param name="parameters"></param>
        /// <returns>
        /// The result of the render operation.
        /// If the template is invalid, the Text property may contain  an error message or an empty string according to the current ExceptionHandlingStrategy setting.
        /// </returns>
        public LavaRenderResult RenderTemplate(ILavaTemplate template, LavaRenderParameters parameters)
        {
            parameters = parameters ?? new LavaRenderParameters();

            LavaRenderResult     result;
            LavaRenderParameters callParameters;

            try
            {
                if (parameters == null)
                {
                    callParameters = new LavaRenderParameters();
                }
                else if (parameters.Context == null)
                {
                    callParameters         = parameters.Clone();
                    callParameters.Context = NewRenderContext();
                }
                else
                {
                    if (parameters.Context.GetType() == typeof(LavaRenderContext))
                    {
                        callParameters = parameters.Clone();

                        // Convert the default context to an engine-specific implementation.
                        var engineContext = NewRenderContext();

                        engineContext.SetInternalFields(parameters.Context.GetInternalFields());
                        engineContext.SetMergeFields(parameters.Context.GetMergeFields());
                        engineContext.SetEnabledCommands(parameters.Context.GetEnabledCommands());

                        // Create a copy of the parameters to ensure the input parameter remains unchanged.
                        callParameters.Context = engineContext;
                    }
                    else
                    {
                        callParameters = parameters;
                    }
                }

                result = OnRenderTemplate(template, callParameters);

                if (result.Error != null)
                {
                    result.Error = GetLavaRenderException(result.Error);
                }
            }
            catch (LavaInterruptException)
            {
                // This exception is intentionally thrown by a component to halt the render process.
                result = new LavaRenderResult();

                result.Text = string.Empty;
            }
            catch (Exception ex)
            {
                if (ex is ThreadAbortException)
                {
                    // Ignore this exception, the calling thread is terminating and no result is required.
                    return(null);
                }

                result = new LavaRenderResult();

                var lre = GetLavaRenderException(ex);

                string message;

                ProcessException(lre, parameters.ExceptionHandlingStrategy, out message);

                result.Error = lre;
                result.Text  = message;
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// Render the provided template using the specified parameters.
        /// </summary>
        /// <param name="inputTemplate"></param>
        /// <param name="parameters"></param>
        /// <returns>
        /// The result of the render operation.
        /// If the template is invalid, the Text property may contain an error message or an empty string according to the current ExceptionHandlingStrategy setting.
        /// </returns>
        public LavaRenderResult RenderTemplate(string inputTemplate, LavaRenderParameters parameters)
        {
            ILavaTemplate        template;
            LavaRenderParameters activeParameters;

            // Copy the render parameters so they can be altered without affecting the input object.
            if (parameters == null)
            {
                activeParameters = new LavaRenderParameters();
            }
            else
            {
                activeParameters = parameters.Clone();
            }

            var renderResult = new LavaRenderResult();

            var exceptionStrategy = activeParameters.ExceptionHandlingStrategy ?? this.ExceptionHandlingStrategy;

            try
            {
                if (_cacheService != null)
                {
                    template = _cacheService.GetOrAddTemplate(this, inputTemplate, activeParameters.CacheKey);
                }
                else
                {
                    template = null;
                }

                bool isParsed = (template != null);

                if (!isParsed)
                {
                    var parseResult = ParseTemplate(inputTemplate);

                    if (parseResult.HasErrors)
                    {
                        throw parseResult.Error;
                    }

                    template = parseResult.Template;
                }

                if (activeParameters.Context == null)
                {
                    activeParameters.Context = NewRenderContext();
                }

                renderResult = RenderTemplate(template, activeParameters);
            }
            catch (Exception ex)
            {
                var lre = GetLavaRenderException(ex, inputTemplate);

                string message;

                if (ex is System.Threading.ThreadAbortException)
                {
                    // If the requesting thread terminated unexpectedly, return an empty string.
                    // This may happen, for example, when a Lava template triggers a page redirect in a web application.
                    message = "{Request aborted}";
                }
                else
                {
                    ProcessException(lre, exceptionStrategy, out message);
                }

                renderResult.Error = ex;
                renderResult.Text  = message;
            }

            return(renderResult);
        }