# HG changeset patch # User cin # Date 1408888626 -14400 # Node ID 82b5641bdba1ce6d46656d2d43816690a4615cfc # Parent 5bca2d201ad87729ec5d88c8a156f3f2633f4fa9 removed nuget diff -r 5bca2d201ad8 -r 82b5641bdba1 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sun Aug 24 17:57:06 2014 +0400 @@ -0,0 +1,3 @@ +syntax: glob +src/Core/RazorEngine.Core/bin/ +src/Core/RazorEngine.Core/obj/ diff -r 5bca2d201ad8 -r 82b5641bdba1 src/Core/RazorEngine.Core/RazorEngine.Core.csproj --- a/src/Core/RazorEngine.Core/RazorEngine.Core.csproj Sun Aug 24 11:14:55 2014 +0400 +++ b/src/Core/RazorEngine.Core/RazorEngine.Core.csproj Sun Aug 24 17:57:06 2014 +0400 @@ -3,8 +3,6 @@ Debug AnyCPU - 8.0.30703 - 2.0 {D268F86D-2DAB-4329-A75F-3BCF6D5BCDC4} Library Properties @@ -36,11 +34,16 @@ True False True - - - - - + + + + + + + + + + ..\..\baseline.xml True Full @@ -71,8 +74,7 @@ - - False + ..\..\packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll @@ -163,7 +165,7 @@ - xcopy "$(TargetDir)RazorEngine.*" "$(SolutionDir)..\nuget\lib\net45" /y + - \ No newline at end of file + diff -r 5bca2d201ad8 -r 82b5641bdba1 src/Core/RazorEngine.Core/Templating/TemplateBase.cs --- a/src/Core/RazorEngine.Core/Templating/TemplateBase.cs Sun Aug 24 11:14:55 2014 +0400 +++ b/src/Core/RazorEngine.Core/Templating/TemplateBase.cs Sun Aug 24 17:57:06 2014 +0400 @@ -1,365 +1,365 @@ -namespace RazorEngine.Templating -{ - using System; - using System.Diagnostics; - using System.Diagnostics.Contracts; - using System.IO; - using System.Text; - - using Text; - - /// - /// Provides a base implementation of a template. - /// - public abstract class TemplateBase : MarshalByRefObject, ITemplate - { - #region Fields - protected ExecuteContext _context; - #endregion - - #region Constructor - /// - /// Initialises a new instance of . - /// - protected TemplateBase() { } - #endregion - - #region Properties - /// - /// Gets or sets the layout template name. - /// - public string Layout { get; set; } - - /// - /// Gets or sets the template service. - /// - public ITemplateService TemplateService { get; set; } - - /// - /// Gets the viewbag that allows sharing state between layout and child templates. - /// - public dynamic ViewBag { get { return _context.ViewBag; } } - - /// - /// Gets the current writer. - /// - public TextWriter CurrentWriter { get { return _context.CurrentWriter; } } - #endregion - - #region Methods - /// - /// Defines a section that can written out to a layout. - /// - /// The name of the section. - /// The delegate used to write the section. - public void DefineSection(string name, Action action) - { - _context.DefineSection(name, action); - } - - /// - /// Includes the template with the specified name. - /// - /// The name of the template type in cache. - /// The model or NULL if there is no model for the template. - /// The template writer helper. - public virtual TemplateWriter Include(string cacheName, object model = null) - { - var instance = TemplateService.Resolve(cacheName, model); - if (instance == null) - throw new ArgumentException("No template could be resolved with name '" + cacheName + "'"); - - return new TemplateWriter(tw => - tw.Write(instance.Run( - TemplateService.CreateExecuteContext(ViewBag)))); - } - - /// - /// Determines if the section with the specified name has been defined. - /// - /// The section name. - /// - public virtual bool IsSectionDefined(string name) - { - if (string.IsNullOrWhiteSpace(name)) - throw new ArgumentException("The name of the section to render must be specified."); - - return (_context.GetSectionDelegate(name) != null); - } - - /// - /// Executes the compiled template. - /// - public virtual void Execute() { } - - /// - /// Returns the specified string as a raw string. This will ensure it is not encoded. - /// - /// The raw string to write. - /// An instance of . - public IEncodedString Raw(string rawString) - { - return new RawString(rawString); - } - - /// - /// Resolves the layout template. - /// - /// The name of the layout template. - /// An instance of . - protected virtual ITemplate ResolveLayout(string name) - { - return TemplateService.Resolve(name, null); - } - - /// - /// Runs the template and returns the result. - /// - /// The current execution context. - /// The merged result of the template. - 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(); - } - - /// - /// Renders the section with the specified name. - /// - /// The name of the section. - /// Flag to specify whether the section is required. - /// The template writer helper. - public virtual TemplateWriter RenderSection(string name, bool isRequired = true) - { - if (string.IsNullOrWhiteSpace(name)) - throw new ArgumentException("The name of the section to render must be specified."); - - var action = _context.GetSectionDelegate(name); - if (action == null && isRequired) - throw new ArgumentException("No section has been defined with name '" + name + "'"); - - if (action == null) action = () => { }; - - return new TemplateWriter(tw => action()); - } - - /// - /// Renders the body of the template. - /// - /// The template writer helper. - public TemplateWriter RenderBody() - { - return _context.PopBody(); - } - - /// - /// Writes the specified object to the result. - /// - /// The value to write. - public virtual void Write(object value) - { - WriteTo(_context.CurrentWriter, value); - } - - /// - /// Writes the specified template helper result. - /// - /// The template writer helper. - public virtual void Write(TemplateWriter helper) - { - if (helper == null) - return; - - helper.WriteTo(_context.CurrentWriter); - } - - /// - /// Writes an attribute to the result. - /// - /// The name of the attribute. - public virtual void WriteAttribute(string name, PositionTagged prefix, PositionTagged suffix, params AttributeValue[] values) - { - WriteAttributeTo(CurrentWriter, name, prefix, suffix, values); - } - - /// - /// Writes an attribute to the specified . - /// - /// The writer. - /// The name of the attribute to be written. - public virtual void WriteAttributeTo(TextWriter writer, string name, PositionTagged prefix, PositionTagged suffix, params AttributeValue[] values) - { - bool first = true; - bool wroteSomething = false; - if (values.Length == 0) - { - // Explicitly empty attribute, so write the prefix and suffix - WritePositionTaggedLiteral(writer, prefix); - WritePositionTaggedLiteral(writer, suffix); - } - else - { - for (int i = 0; i < values.Length; i++) - { - AttributeValue attrVal = values[i]; - PositionTagged val = attrVal.Value; - - bool? boolVal = null; - if (val.Value is bool) - { - boolVal = (bool)val.Value; - } - - if (val.Value != null && (boolVal == null || boolVal.Value)) - { - string valStr = val.Value as string; - if (valStr == null) - { - valStr = val.Value.ToString(); - } - if (boolVal != null) - { - Debug.Assert(boolVal.Value); - valStr = name; - } - - if (first) - { - WritePositionTaggedLiteral(writer, prefix); - first = false; - } - else - { - WritePositionTaggedLiteral(writer, attrVal.Prefix); - } - - if (attrVal.Literal) - { - WriteLiteralTo(writer, valStr); - } - else - { - WriteTo(writer, valStr); // Write value - } - wroteSomething = true; - } - } - if (wroteSomething) - { - WritePositionTaggedLiteral(writer, suffix); - } - } - } - - /// - /// Writes the specified string to the result. - /// - /// The literal to write. - public virtual void WriteLiteral(string literal) - { - WriteLiteralTo(_context.CurrentWriter, literal); - } - - /// - /// Writes a string literal to the specified . - /// - /// The writer. - /// The literal to be written. - public virtual void WriteLiteralTo(TextWriter writer, string literal) - { - if (writer == null) - throw new ArgumentNullException("writer"); - - if (literal == null) return; - writer.Write(literal); - } - - /// - /// Writes a literal to the result. - /// - /// The writer. - /// The literal to be written. - private void WritePositionTaggedLiteral(TextWriter writer, PositionTagged value) - { - WriteLiteralTo(writer, value.Value); - } - - /// - /// Writes the specified object to the specified . - /// - /// The writer. - /// The value to be written. - public virtual void WriteTo(TextWriter writer, object value) - { - if (writer == null) - throw new ArgumentNullException("writer"); - - if (value == null) return; - - var encodedString = value as IEncodedString; - if (encodedString != null) - { - writer.Write(encodedString); - } - else - { - encodedString = TemplateService.EncodedStringFactory.CreateEncodedString(value); - _context.CurrentWriter.Write(encodedString); - } - } - - /// - /// Writes the specfied template helper result to the specified writer. - /// - /// The writer. - /// The template writer helper. - public virtual void WriteTo(TextWriter writer, TemplateWriter helper) - { - if (helper == null) return; - - helper.WriteTo(writer); - } - - /// - /// Resolves the specified path - /// - /// The path. - /// The resolved path. - public virtual string ResolveUrl(string path) - { - // TODO: Actually resolve the url - if (path.StartsWith("~")) - { - path = path.Substring(1); - } - return path; - } - #endregion - } +namespace RazorEngine.Templating +{ + using System; + using System.Diagnostics; + using System.Diagnostics.Contracts; + using System.IO; + using System.Text; + + using Text; + + /// + /// Provides a base implementation of a template. + /// + public abstract class TemplateBase : MarshalByRefObject, ITemplate + { + #region Fields + protected ExecuteContext _context; + #endregion + + #region Constructor + /// + /// Initialises a new instance of . + /// + protected TemplateBase() { } + #endregion + + #region Properties + /// + /// Gets or sets the layout template name. + /// + public string Layout { get; set; } + + /// + /// Gets or sets the template service. + /// + public ITemplateService TemplateService { get; set; } + + /// + /// Gets the viewbag that allows sharing state between layout and child templates. + /// + public dynamic ViewBag { get { return _context.ViewBag; } } + + /// + /// Gets the current writer. + /// + public TextWriter CurrentWriter { get { return _context.CurrentWriter; } } + #endregion + + #region Methods + /// + /// Defines a section that can written out to a layout. + /// + /// The name of the section. + /// The delegate used to write the section. + public void DefineSection(string name, Action action) + { + _context.DefineSection(name, action); + } + + /// + /// Includes the template with the specified name. + /// + /// The name of the template type in cache. + /// The model or NULL if there is no model for the template. + /// The template writer helper. + public virtual TemplateWriter Include(string cacheName, object model = null) + { + var instance = TemplateService.Resolve(cacheName, model); + if (instance == null) + throw new ArgumentException("No template could be resolved with name '" + cacheName + "'"); + + return new TemplateWriter(tw => + tw.Write(instance.Run( + TemplateService.CreateExecuteContext(ViewBag)))); + } + + /// + /// Determines if the section with the specified name has been defined. + /// + /// The section name. + /// + public virtual bool IsSectionDefined(string name) + { + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentException("The name of the section to render must be specified."); + + return (_context.GetSectionDelegate(name) != null); + } + + /// + /// Executes the compiled template. + /// + public virtual void Execute() { } + + /// + /// Returns the specified string as a raw string. This will ensure it is not encoded. + /// + /// The raw string to write. + /// An instance of . + public IEncodedString Raw(string rawString) + { + return new RawString(rawString); + } + + /// + /// Resolves the layout template. + /// + /// The name of the layout template. + /// An instance of . + protected virtual ITemplate ResolveLayout(string name) + { + return TemplateService.Resolve(name, null); + } + + /// + /// Runs the template and returns the result. + /// + /// The current execution context. + /// The merged result of the template. + 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(); + } + + /// + /// Renders the section with the specified name. + /// + /// The name of the section. + /// Flag to specify whether the section is required. + /// The template writer helper. + public virtual TemplateWriter RenderSection(string name, bool isRequired = true) + { + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentException("The name of the section to render must be specified."); + + var action = _context.GetSectionDelegate(name); + if (action == null && isRequired) + throw new ArgumentException("No section has been defined with name '" + name + "'"); + + if (action == null) action = () => { }; + + return new TemplateWriter(tw => action()); + } + + /// + /// Renders the body of the template. + /// + /// The template writer helper. + public TemplateWriter RenderBody() + { + return _context.PopBody(); + } + + /// + /// Writes the specified object to the result. + /// + /// The value to write. + public virtual void Write(object value) + { + WriteTo(_context.CurrentWriter, value); + } + + /// + /// Writes the specified template helper result. + /// + /// The template writer helper. + public virtual void Write(TemplateWriter helper) + { + if (helper == null) + return; + + helper.WriteTo(_context.CurrentWriter); + } + + /// + /// Writes an attribute to the result. + /// + /// The name of the attribute. + public virtual void WriteAttribute(string name, PositionTagged prefix, PositionTagged suffix, params AttributeValue[] values) + { + WriteAttributeTo(CurrentWriter, name, prefix, suffix, values); + } + + /// + /// Writes an attribute to the specified . + /// + /// The writer. + /// The name of the attribute to be written. + public virtual void WriteAttributeTo(TextWriter writer, string name, PositionTagged prefix, PositionTagged suffix, params AttributeValue[] values) + { + bool first = true; + bool wroteSomething = false; + if (values.Length == 0) + { + // Explicitly empty attribute, so write the prefix and suffix + WritePositionTaggedLiteral(writer, prefix); + WritePositionTaggedLiteral(writer, suffix); + } + else + { + for (int i = 0; i < values.Length; i++) + { + AttributeValue attrVal = values[i]; + PositionTagged val = attrVal.Value; + + bool? boolVal = null; + if (val.Value is bool) + { + boolVal = (bool)val.Value; + } + + if (val.Value != null && (boolVal == null || boolVal.Value)) + { + string valStr = val.Value as string; + if (valStr == null) + { + valStr = val.Value.ToString(); + } + if (boolVal != null) + { + Debug.Assert(boolVal.Value); + valStr = name; + } + + if (first) + { + WritePositionTaggedLiteral(writer, prefix); + first = false; + } + else + { + WritePositionTaggedLiteral(writer, attrVal.Prefix); + } + + if (attrVal.Literal) + { + WriteLiteralTo(writer, valStr); + } + else + { + WriteTo(writer, valStr); // Write value + } + wroteSomething = true; + } + } + if (wroteSomething) + { + WritePositionTaggedLiteral(writer, suffix); + } + } + } + + /// + /// Writes the specified string to the result. + /// + /// The literal to write. + public virtual void WriteLiteral(string literal) + { + WriteLiteralTo(_context.CurrentWriter, literal); + } + + /// + /// Writes a string literal to the specified . + /// + /// The writer. + /// The literal to be written. + public virtual void WriteLiteralTo(TextWriter writer, string literal) + { + if (writer == null) + throw new ArgumentNullException("writer"); + + if (literal == null) return; + writer.Write(literal); + } + + /// + /// Writes a literal to the result. + /// + /// The writer. + /// The literal to be written. + private void WritePositionTaggedLiteral(TextWriter writer, PositionTagged value) + { + WriteLiteralTo(writer, value.Value); + } + + /// + /// Writes the specified object to the specified . + /// + /// The writer. + /// The value to be written. + public virtual void WriteTo(TextWriter writer, object value) + { + if (writer == null) + throw new ArgumentNullException("writer"); + + if (value == null) return; + + var encodedString = value as IEncodedString; + if (encodedString != null) + { + writer.Write(encodedString); + } + else + { + encodedString = TemplateService.EncodedStringFactory.CreateEncodedString(value); + _context.CurrentWriter.Write(encodedString); + } + } + + /// + /// Writes the specfied template helper result to the specified writer. + /// + /// The writer. + /// The template writer helper. + public virtual void WriteTo(TextWriter writer, TemplateWriter helper) + { + if (helper == null) return; + + helper.WriteTo(writer); + } + + /// + /// Resolves the specified path + /// + /// The path. + /// The resolved path. + public virtual string ResolveUrl(string path) + { + // TODO: Actually resolve the url + if (path.StartsWith("~")) + { + path = path.Substring(1); + } + return path; + } + #endregion + } } \ No newline at end of file