Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Updated HtmlHelpers<T> and UrlHelpers<T> #592

Merged
merged 5 commits into from
Apr 21, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/Nancy.ViewEngines.Razor/HtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,45 @@
{
using System;
using System.IO;
using System.Linq.Expressions;

/// <summary>
/// Helpers to generate html content.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
public class HtmlHelpers<TModel> : IHtmlHelpers<TModel>
public class HtmlHelpers<TModel>
{
private readonly TModel model;
private readonly RazorViewEngine engine;
private readonly IRenderContext renderContext;

/// <summary>
/// Initializes a new instance of the <see cref="HtmlHelpers{t}"/> class.
/// Initializes a new instance of the <see cref="HtmlHelpers{T}"/> class.
/// </summary>
/// <param name="engine">The razor view engine instance that the helpers are being used by.</param>
/// <param name="renderContext">The <see cref="IRenderContext"/> that the helper are being used by.</param>
/// <param name="model">The model that is used by the page where the helpers are invoked.</param>
public HtmlHelpers(RazorViewEngine engine, IRenderContext renderContext, TModel model)
{
this.engine = engine;
this.renderContext = renderContext;
this.model = model;
this.Engine = engine;
this.RenderContext = renderContext;
this.Model = model;
}

/// <summary>
/// The model that is being used by the current view.
/// </summary>
/// <value>An instance of the view model.</value>
public TModel Model { get; set; }

/// <summary>
/// The engine that is currently rendering the view.
/// </summary>
/// <value>A <see cref="RazorViewEngine"/> instance.</value>
public RazorViewEngine Engine { get; set; }

/// <summary>
/// The context of the current render operation.
/// </summary>
/// <value>An <see cref="IRenderContext"/> intance.</value>
public IRenderContext RenderContext { get; set; }

/// <summary>
/// Renders a partial with the given view name.
/// </summary>
Expand All @@ -44,9 +59,9 @@ public IHtmlString Partial(string viewName)
/// <returns>An <see cref="IHtmlString"/> representation of the partial.</returns>
public IHtmlString Partial(string viewName, dynamic modelForPartial)
{
var view = this.renderContext.LocateView(viewName, modelForPartial);
var view = this.RenderContext.LocateView(viewName, modelForPartial);

var response = this.engine.RenderView(view, modelForPartial, this.renderContext);
var response = this.Engine.RenderView(view, modelForPartial, this.RenderContext);
Action<Stream> action = response.Contents;
var mem = new MemoryStream();

Expand Down Expand Up @@ -74,7 +89,8 @@ public IHtmlString Raw(string text)
/// <returns>An <see cref="IHtmlString"/> representation of the anti forgery token.</returns>
public IHtmlString AntiForgeryToken()
{
var tokenKeyValue = this.renderContext.GetCsrfToken();
var tokenKeyValue =
this.RenderContext.GetCsrfToken();

return new NonEncodedHtmlString(String.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\"/>", tokenKeyValue.Key, tokenKeyValue.Value));
}
Expand Down
37 changes: 0 additions & 37 deletions src/Nancy.ViewEngines.Razor/IHtmlHelpers.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/Nancy.ViewEngines.Razor/IUrlHelpers.cs

This file was deleted.

2 changes: 0 additions & 2 deletions src/Nancy.ViewEngines.Razor/Nancy.ViewEngines.Razor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@
<Compile Include="CSharp\CSharpRazorViewRenderer.cs" />
<Compile Include="DefaultRazorConfiguration.cs" />
<Compile Include="HtmlHelpers.cs" />
<Compile Include="IHtmlHelpers.cs" />
<Compile Include="IHtmlString.cs" />
<Compile Include="IRazorConfiguration.cs" />
<Compile Include="IRazorViewRenderer.cs" />
<Compile Include="IUrlHelpers.cs" />
<Compile Include="ModelFinder.cs" />
<Compile Include="ModelSpan.cs" />
<Compile Include="CSharp\NancyCSharpRazorCodeGenerator.cs" />
Expand Down
10 changes: 3 additions & 7 deletions src/Nancy.ViewEngines.Razor/NancyRazorViewBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Text;

using Nancy.Helpers;

/// <summary>
Expand Down Expand Up @@ -45,10 +44,7 @@ public abstract class NancyRazorViewBase
/// </value>
public bool HasLayout
{
get
{
return !String.IsNullOrEmpty(this.Layout);
}
get { return !String.IsNullOrEmpty(this.Layout); }
}

/// <summary>
Expand Down Expand Up @@ -225,7 +221,7 @@ public abstract class NancyRazorViewBase<TModel> : NancyRazorViewBase
/// <summary>
/// Gets the Html helper.
/// </summary>
public IHtmlHelpers<TModel> Html { get; private set; }
public HtmlHelpers<TModel> Html { get; private set; }

/// <summary>
/// Gets the model.
Expand All @@ -235,7 +231,7 @@ public abstract class NancyRazorViewBase<TModel> : NancyRazorViewBase
/// <summary>
/// Gets the Url helper.
/// </summary>
public IUrlHelpers<TModel> Url { get; private set; }
public UrlHelpers<TModel> Url { get; private set; }

/// <summary>
/// Non-model specific data for rendering in the response
Expand Down
5 changes: 5 additions & 0 deletions src/Nancy.ViewEngines.Razor/NonEncodedHtmlString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public string ToHtmlString()
{
return value;
}

public static implicit operator NonEncodedHtmlString(string value)
{
return new NonEncodedHtmlString(value);
}
}
}
23 changes: 16 additions & 7 deletions src/Nancy.ViewEngines.Razor/UrlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,38 @@ namespace Nancy.ViewEngines.Razor
/// Helpers for url related functions.
/// </summary>
/// <typeparam name="TModel">The type of the model.</typeparam>
public class UrlHelpers<TModel> : IUrlHelpers<TModel>
public class UrlHelpers<TModel>
{
private readonly RazorViewEngine razorViewEngine;
private readonly IRenderContext renderContext;

/// <summary>
/// Initializes a new instance of the <see cref="UrlHelpers&lt;TModel&gt;"/> class.
/// </summary>
/// <param name="razorViewEngine">The razor view engine.</param>
/// <param name="renderContext">The render context.</param>
public UrlHelpers(RazorViewEngine razorViewEngine, IRenderContext renderContext)
{
this.razorViewEngine = razorViewEngine;
this.renderContext = renderContext;
this.RazorViewEngine = razorViewEngine;
this.RenderContext = renderContext;
}

/// <summary>
/// The engine that is currently rendering the view.
/// </summary>
/// <value>A <see cref="RazorViewEngine"/> instance.</value>
public RazorViewEngine RazorViewEngine { get; set; }

/// <summary>
/// The context of the current render operation.
/// </summary>
/// <value>An <see cref="IRenderContext"/> intance.</value>
public IRenderContext RenderContext { get; set; }

/// <summary>
/// Retrieves the absolute url of the specified path.
/// </summary>
/// <param name="path">The path.</param>
public string Content(string path)
{
return renderContext.ParsePath(path);
return this.RenderContext.ParsePath(path);
}
}
}