Beispiel #1
0
        /// <summary>
        /// Renders the code block specified into a string
        /// </summary>
        /// <param name="context">The template context</param>
        /// <param name="block">The code block to render</param>
        /// <param name="flags">The template flags</param>
        /// <returns>The rendered block</returns>
        public string Render
        (
            ref TemplateContext context,
            CodeBlock block,
            params TemplateFlag[] flags
        )
        {
            Validate.IsNotNull(block);

            var loop = (ForEachLoop)block;

            var collection = ResolveValue
                             (
                ref context,
                loop.CollectionValue,
                loop.CollectionType
                             );

            if (collection == null)
            {
                throw new NettleRenderException
                      (
                          "A null collection was invoked at index {0}.".With
                          (
                              loop.StartPosition
                          )
                      );
            }
            else if (false == collection.GetType().IsEnumerable())
            {
                throw new NettleRenderException
                      (
                          "The type {0} is not a valid collection.".With
                          (
                              collection.GetType().Name
                          )
                      );
            }

            var builder = new StringBuilder();

            foreach (var item in collection as IEnumerable)
            {
                var nestedContext = context.CreateNestedContext
                                    (
                    item
                                    );

                var renderedContent = _collectionRenderer.Render
                                      (
                    ref nestedContext,
                    loop.Blocks,
                    flags
                                      );

                builder.Append(renderedContent);
            }

            return(builder.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Renders the code block specified into a string
        /// </summary>
        /// <param name="context">The template context</param>
        /// <param name="block">The code block to render</param>
        /// <param name="flags">The template flags</param>
        /// <returns>The rendered block</returns>
        public string Render
        (
            ref TemplateContext context,
            CodeBlock block,
            params TemplateFlag[] flags
        )
        {
            Validate.IsNotNull(block);

            var partial = (RenderPartial)block;

            CheckForCircularReference
            (
                ref context,
                partial
            );

            var template = _templateRepository.Get
                           (
                partial.TemplateName
                           );

            var partialContent = template.ParsedTemplate.Blocks;
            var model          = context.Model;

            if (partial.ModelType.HasValue && partial.ModelValue != null)
            {
                model = ResolveValue
                        (
                    ref context,
                    partial.ModelValue,
                    partial.ModelType.Value
                        );
            }

            var newContext = context.CreateNestedContext
                             (
                model
                             );

            newContext.Variables.Clear();

            newContext.PartialCallStack.Add
            (
                partial.TemplateName
            );

            return(_collectionRenderer.Render
                   (
                       ref newContext,
                       partialContent,
                       flags
                   ));
        }
Beispiel #3
0
        /// <summary>
        /// Renders a Nettle template with the model data specified
        /// </summary>
        /// <param name="template">The template</param>
        /// <param name="model">The model data</param>
        /// <returns>The rendered template</returns>
        public string Render
        (
            Template template,
            object model
        )
        {
            Validate.IsNotNull(template);

            var watch = Stopwatch.StartNew();

            var debugMode = template.IsFlagSet
                            (
                TemplateFlag.DebugMode
                            );

            var context = new TemplateContext
                          (
                model,
                template.Flags
                          );

            var blocks = template.Blocks;

            var output = _collectionRenderer.Render
                         (
                ref context,
                blocks,
                template.Flags
                         );

            if (debugMode)
            {
                watch.Stop();

                var debugInfo = context.GenerateDebugInfo
                                (
                    watch.Elapsed
                                );

                output += "\r\n\r\n{0}".With
                          (
                    debugInfo
                          );
            }

            return(output);
        }
Beispiel #4
0
        /// <summary>
        /// Renders the code block specified into a string
        /// </summary>
        /// <param name="context">The template context</param>
        /// <param name="block">The code block to render</param>
        /// <param name="flags">The template flags</param>
        /// <returns>The rendered block</returns>
        public string Render
        (
            ref TemplateContext context,
            CodeBlock block,
            params TemplateFlag[] flags
        )
        {
            Validate.IsNotNull(block);

            var loop = (WhileLoop)block;

            var result = _expressionEvaluator.Evaluate
                         (
                ref context,
                loop.ConditionExpression
                         );

            var builder = new StringBuilder();

            while (result)
            {
                var renderedBody = _collectionRenderer.Render
                                   (
                    ref context,
                    loop.Blocks,
                    flags
                                   );

                builder.Append(renderedBody);

                result = _expressionEvaluator.Evaluate
                         (
                    ref context,
                    loop.ConditionExpression
                         );
            }

            return(builder.ToString());
        }
Beispiel #5
0
        /// <summary>
        /// Renders the code block specified into a string
        /// </summary>
        /// <param name="context">The template context</param>
        /// <param name="block">The code block to render</param>
        /// <param name="flags">The template flags</param>
        /// <returns>The rendered block</returns>
        public string Render
        (
            ref TemplateContext context,
            CodeBlock block,
            params TemplateFlag[] flags
        )
        {
            Validate.IsNotNull(block);

            var statement    = (IfStatement)block;
            var renderedBody = String.Empty;

            var result = _expressionEvaluator.Evaluate
                         (
                ref context,
                statement.ConditionExpression
                         );

            if (result)
            {
                renderedBody = _collectionRenderer.Render
                               (
                    ref context,
                    statement.Blocks,
                    flags
                               );
            }
            else if (statement.ElseIfConditions.Any())
            {
                foreach (var elseCondition in statement.ElseIfConditions)
                {
                    result = _expressionEvaluator.Evaluate
                             (
                        ref context,
                        elseCondition.ConditionExpression
                             );

                    if (result)
                    {
                        renderedBody = _collectionRenderer.Render
                                       (
                            ref context,
                            elseCondition.Blocks,
                            flags
                                       );

                        break;
                    }
                }
            }

            if (false == result && statement.ElseContent != null)
            {
                renderedBody = _collectionRenderer.Render
                               (
                    ref context,
                    statement.ElseContent.Blocks,
                    flags
                               );
            }

            return(renderedBody);
        }