Beispiel #1
0
        /// <summary>
        /// Create a new template context containing the specified merge fields.
        /// </summary>
        /// <param name="mergeFields"></param>
        /// <returns></returns>

        protected override ILavaRenderContext OnCreateRenderContext()
        {
            // Create a new DotLiquid Context.
            // Set the flag to rethrow exceptions generated by the DotLiquid framework, so they can be intercepted and handled as Lava errors.
            var dotLiquidContext = new global::DotLiquid.Context(new List <Hash>(), new Hash(), new Hash(), rethrowErrors: true);

            var context = new DotLiquidRenderContext(dotLiquidContext);

            return(context);
        }
        /// <summary>
        /// Render a specific instance of the block in the provided context.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="result"></param>
        public override void Render(Context context, TextWriter result)
        {
            var lavaContext = new DotLiquidRenderContext(context);

            var block = _lavaBlock as ILiquidFrameworkElementRenderer;

            if (block == null)
            {
                throw new Exception("Block proxy cannot be rendered.");
            }

            // Call the renderer implemented by the wrapped Lava block.
            block.Render(this, lavaContext, result, null);
        }
Beispiel #3
0
        /// <summary>
        /// Translate a set of DotLiquid filter arguments to a set of arguments that are compatible with a Lava filter.
        /// </summary>
        private void GetLavaFilterCompatibleArguments(string filterName, List <object> args, ParameterInfo[] lavaFilterFunctionParams, Context dotLiquidContext)
        {
            // Unwrap proxy objects.
            for (int i = 0; i < args.Count; i++)
            {
                if (args[i] is ILiquidFrameworkDataObjectProxy proxy)
                {
                    args[i] = proxy.GetProxiedDataObject();
                }

                if (args[i] is DropProxy)
                {
                    args[i] = (( DropProxy )args[i]).ConvertToValueType();
                }
            }

            // Inject the render context if it is requested.
            if (lavaFilterFunctionParams.Length > 0 && lavaFilterFunctionParams[0].ParameterType == typeof(ILavaRenderContext))
            {
                var renderContext = new DotLiquidRenderContext(dotLiquidContext);

                InitializeRenderContext(renderContext);

                args.Insert(0, renderContext);
            }

            // Add in any missing parameters with the default values defined for the filter method.
            if (lavaFilterFunctionParams.Length > args.Count)
            {
                for (int i = args.Count; i < lavaFilterFunctionParams.Length; ++i)
                {
                    if ((lavaFilterFunctionParams[i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault)
                    {
                        throw new LavaException("Error - Filter '{0}' does not have a default value for '{1}' and no value was supplied", filterName, lavaFilterFunctionParams[i].Name);
                    }
                    args.Add(lavaFilterFunctionParams[i].DefaultValue);
                }
            }
        }