Example #1
0
 /// <summary>
 /// Writes a value to the specified <see cref="TextWriter" />.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="lambdaWriter">The lambda writer.</param>
 /// <exception cref="System.ArgumentNullException">writer</exception>
 public virtual void WriteTo(TextWriter writer, LambdaWriter lambdaWriter)
 {
     if (writer == null) throw new ArgumentNullException("writer");
     if (lambdaWriter == null) throw new ArgumentNullException("lambdaWriter");
     lambdaWriter.WriteTo(writer);
 }
Example #2
0
        /// <summary>
        /// Runs the templating.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>The result of templating.</returns>
        /// <exception cref="System.InvalidOperationException">If a layout was not found</exception>
        public virtual string Run(PageTemplateContext context)
        {
            try
            {
                var writer = new StringWriter();

                // Execute this template
                Context = context;
                context.Writer = writer;
                Execute();
                Context.Writer = null;

                // If we are in a layout context, use the current layout
                if (Layout != null)
                {
                    // Get the layout template.
                    var layout = FindTemplate(Layout);
                    
                    if (layout == null)
                    {
                        throw new InvalidOperationException(
                            string.Format("Layout [{0}] was not found in registered template", Layout));
                    }

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

                    return layout.Run(context);
                }

                return writer.ToString();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Writes to the current <see cref="Writer"/> using the specified lambda writer.
        /// </summary>
        /// <param name="lambdaWriter">The lambda writer.</param>
        public virtual void Write(LambdaWriter lambdaWriter)
        {
            if (lambdaWriter == null)
                return;

            WriteTo(Writer, lambdaWriter);
        }
        /// <summary>
        /// Pushes the body of a layout that will be rendered later (at <see cref="PopBody"/> time).
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <exception cref="System.ArgumentNullException">writer</exception>
        public virtual void PushBody(LambdaWriter writer)
        {
            if (writer == null) throw new ArgumentNullException("writer");

            lambdaWriters.Push(writer);
        }