void ITemplate.Run(ExecuteContext context, TextWriter textWriter)
 {
     var builder = new StringBuilder();
     _executeContextAdapter = new ExecuteContextAdapter(this, context);
     using (var writer = new StringWriter(builder))
     {
         _executeContextAdapter.CurrentWriter = writer;
         OnStart();
         Execute();
         OnEnd();
         _executeContextAdapter.CurrentWriter = null;
     }
     var parent = ResolveLayout(Layout);
     if (parent == null && string.IsNullOrEmpty(Layout))
     {
         var result = builder.ToString();
         textWriter.Write(result);
         return;
     }
     if (parent == null)
     {
         throw new InvalidOperationException("Layout template was not found.");
     }
     parent.SetData(null, ViewBag);
     var exposingParent = parent as ExposingTemplate;
     if (exposingParent == null)
     {
         throw new InvalidOperationException("Unexpected layout template base type.");
     }
     exposingParent._templateVisitor = _templateVisitor;
     var bodyWriter = new TemplateWriter(tw => tw.Write(builder.ToString()));
     _executeContextAdapter.PushBody(bodyWriter);
     _executeContextAdapter.PushSections();
     parent.Run(_executeContextAdapter.Context, textWriter);
 }
Example #2
0
        /// <summary>
        /// Pushes the specified template writer helper onto the stack.
        /// </summary>
        /// <param name="bodyWriter">The template writer helper.</param>
        internal void PushBody(TemplateWriter bodyWriter)
        {
            if (bodyWriter == null)
                throw new ArgumentNullException("bodyWriter");

            _bodyWriters.Push(bodyWriter);
        }
 internal void PushBody(TemplateWriter templateWriter)
 {
     if (_pushBodyMethod == null)
     {
         throw new InvalidOperationException("PushBody method was not found.");
     }
     _pushBodyMethod.Invoke(_context, new object[] {templateWriter});
 }
Example #4
0
        /// <summary>
        /// Writes the specfied template helper result to the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="helper">The template writer helper.</param>
        public virtual void WriteTo(TextWriter writer, TemplateWriter helper)
        {
            if (helper == null)
            {
                return;
            }

            helper.WriteTo(writer);
        }
Example #5
0
        /// <summary>
        /// Writes the specified template helper result.
        /// </summary>
        /// <param name="helper">The template writer helper.</param>
        public virtual void Write(TemplateWriter helper)
        {
            if (helper == null)
            {
                return;
            }

            helper.WriteTo(_context.CurrentWriter);
        }
Example #6
0
        /// <summary>
        /// Pushes the specified template writer helper onto the stack.
        /// </summary>
        /// <param name="bodyWriter">The template writer helper.</param>
        internal void PushBody(TemplateWriter bodyWriter)
        {
            if (bodyWriter == null)
            {
                throw new ArgumentNullException("bodyWriter");
            }

            _bodyWriters.Push(bodyWriter);
        }
Example #7
0
        void ITemplate.Run(ExecuteContext context, TextWriter reader)
#endif
        {
            _context = context;

            using (var memory = new MemoryStream())
            {
                using (var writer = new StreamWriter(memory))
                {
                    _context.CurrentWriter = writer;
#if RAZOR4
                    await Execute();
#else
                    Execute();
#endif
                    writer.Flush();
                    _context.CurrentWriter = null;


                    if (Layout != null)
                    {
                        // Get the layout template.
                        var layout = ResolveLayout(Layout);

                        if (layout == null)
                        {
                            throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver.");
                        }

                        // Push the current body instance onto the stack for later execution.
                        var body = new TemplateWriter(tw =>
                        {
                            StreamToTextWriter(memory, tw);
                        });
                        context.PushBody(body);
                        context.PushSections();

#if RAZOR4
                        await layout.Run(context, reader);
#else
                        layout.Run(context, reader);
#endif
                        return;
                    }

                    StreamToTextWriter(memory, reader);
                }
            }
        }
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <param name="reader"></param>
        /// <returns>The merged result of the template.</returns>

        public async Task Run(ExecuteContext context, TextWriter reader)
        {
            _context = context;

            StringBuilder builder = new StringBuilder();

            using (var writer = new StringWriter(builder))
            {
                _context.CurrentWriter = writer;
                await ExecuteAsync();

                writer.Flush();
                _context.CurrentWriter = null;


                if (Layout != null)
                {
                    // Get the layout template.
                    var layout = ResolveLayout(Layout);

                    if (layout == null)
                    {
                        throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver.");
                    }

                    // Push the current body instance onto the stack for later execution.
                    var body = new TemplateWriter(tw => tw.Write(builder.ToString()));
                    context.PushBody(body);
                    context.PushSections();

                    await layout.Run(context, reader);

                    return;
                }

                reader.Write(builder.ToString());
            }
        }
Example #9
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <returns>The merged result of the template.</returns>
        string ITemplate.Run(ExecuteContext context)
        {
            _context = context;

            var builder = new StringBuilder();

            using (var writer = new StringWriter(builder))
            {
                _context.CurrentWriter = writer;
                Execute();
                _context.CurrentWriter = null;
            }

            if (_Layout != null)
            {
                var layout = TemplateService.Resolve(_Layout);
                var body   = new TemplateWriter(tw => tw.Write(builder.ToString()));
                context.PushBody(body);

                return(layout.Run(context));
            }

            return(builder.ToString());
        }
Example #10
0
 /// <summary>
 /// Writes the specfied template helper result to the specified writer.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="helper">The template writer helper.</param>
 public virtual void WriteTo(TextWriter writer, TemplateWriter helper)
 {
     helper.WriteTo(writer);
 }
Example #11
0
        /// <summary>
        /// Writes the specified template helper result.
        /// </summary>
        /// <param name="helper">The template writer helper.</param>
        public virtual void Write(TemplateWriter helper)
        {
            if (helper == null)
                return;

            helper.WriteTo(_context.CurrentWriter);
        }
Example #12
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <returns>The merged result of the template.</returns>
        string ITemplate.Run(ExecuteContext context)
        {
            _context = context;

            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder))
            {
                _context.CurrentWriter = writer;
                Execute();
                _context.CurrentWriter = null;
            }

            if (Layout != null)
            {
                // Get the layout template.
                var layout = ResolveLayout(Layout);

                // Push the current body instance onto the stack for later execution.
                var body = new TemplateWriter(tw => tw.Write(builder.ToString()));
                context.PushBody(body);

                return layout.Run(context);
            }

            return builder.ToString();
        }
Example #13
0
        /// <summary>
        /// Runs the template and returns the result.
        /// </summary>
        /// <param name="context">The current execution context.</param>
        /// <returns>The merged result of the template.</returns>
        string ITemplate.Run(ExecuteContext context)
        {
            _context = context;

            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder))
            {
                _context.CurrentWriter = writer;
                Execute();
                _context.CurrentWriter = null;
            }

            if (Layout != null)
            {
                // Get the layout template.
                var layout = ResolveLayout(Layout);

                if (layout == null)
                {
                    throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver.");
                }

                // Push the current body instance onto the stack for later execution.
                var body = new TemplateWriter(tw => tw.Write(builder.ToString()));
                context.PushBody(body);

                return layout.Run(context);
            }

            return builder.ToString();
        }
Example #14
0
 /// <summary>
 /// Writes the specfied template helper result to the specified writer.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="helper">The template writer helper.</param>
 public virtual void WriteTo(TextWriter writer, TemplateWriter helper)
 {
     helper.WriteTo(writer);
 }
Example #15
0
        /// <summary>
        /// Writes the specfied template helper result to the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="helper">The template writer helper.</param>
        public virtual void WriteTo(TextWriter writer, TemplateWriter helper)
        {
            if (helper == null) return;

            helper.WriteTo(writer);
        }
Example #16
0
        void ITemplate.Run(ExecuteContext context, TextWriter reader)
#endif
        {
            _context = context;

            using (var memory = new MemoryStream())
            {
                using (var writer = new StreamWriter(memory))
                {
                    _context.CurrentWriter = writer;
#if RAZOR4
                    await Execute();
#else
                    Execute();
#endif
                    writer.Flush();
                    _context.CurrentWriter = null;


                    if (Layout != null)
                    {
                        // Get the layout template.
                        var layout = ResolveLayout(Layout);

                        if (layout == null)
                        {
                            throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver.");
                        }

                        // Push the current body instance onto the stack for later execution.
                        var body = new TemplateWriter(tw =>
                        {
                            StreamToTextWriter(memory, tw);
                        });
                        context.PushBody(body);
                        context.PushSections();

#if RAZOR4
                        await layout.Run(context, reader);
#else
                        layout.Run(context, reader);
#endif
                    return;
                    }

                    StreamToTextWriter(memory, reader);
                }
            }
        }
 public static void WriteTo(TextWriter writer, TemplateWriter helper)
 {
     helper.WriteTo(writer);
 }
Example #18
0
 public static void WriteTo(TextWriter writer, TemplateWriter helper)
 {
     helper.WriteTo(writer);
 }
Example #19
0
 /// <summary>
 /// Writes the specified template helper result.
 /// </summary>
 /// <param name="helper">The template writer helper.</param>
 public virtual void Write(TemplateWriter helper)
 {
     helper.WriteTo(_context.CurrentWriter);
 }