Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created August 23, 2012 19:03
Show Gist options
  • Select an option

  • Save Buildstarted/3440372 to your computer and use it in GitHub Desktop.

Select an option

Save Buildstarted/3440372 to your computer and use it in GitHub Desktop.
Nancy implementation for the parrot view engine
public class NancyDepdencyResolver : IDependencyResolver
{
private TinyIoCContainer _container;
private DependencyResolver _resolver;
public NancyDepdencyResolver(TinyIoCContainer container)
{
_container = container;
_resolver = new DependencyResolver();
}
public void Register(Type type, Func<object> activator)
{
_resolver.Register(type, activator);
}
public object Get(Type type)
{
return _container.Resolve(type) ?? _resolver.Get(type);
}
public T Get<T>() where T : class
{
return _container.Resolve<T>() ?? _resolver.Get<T>();
}
}
namespace Nancy.ViewEngines.Parrot
{
using Parrot.Infrastructure;
using Parrot.Nodes;
using Parrot.Renderers;
using Parrot.Renderers.Infrastructure;
public class NancyHost : Host
{
public NancyHost() : this(new DependencyResolver()) { }
public NancyHost(IDependencyResolver resolver) : base(resolver)
{
InitializeRendererFactory();
DependencyResolver.Register(typeof(DocumentRenderer), () => new DocumentRenderer(this));
DependencyResolver.Register(typeof (IModelValueProviderFactory), () => new ModelValueProviderFactory());
}
private void InitializeRendererFactory()
{
var factory = new RendererFactory(this);
DependencyResolver.Register(typeof(IRendererFactory), () => factory);
}
}
}
namespace Nancy.ViewEngines.Parrot
{
using System.IO;
using Responses;
using global::Parrot.Infrastructure;
using global::Parrot.Nodes;
using global::Parrot.Parser;
using NancyTest;
using global::Parrot.Renderers;
public class ParrotViewEngine : IViewEngine
{
private readonly IHost _host;
public ParrotViewEngine() : this(new NancyHost())
{
}
public ParrotViewEngine(IHost host)
{
_host = host;
}
public IEnumerable<string> Extensions
{
get { yield return "parrot"; }
}
public void Initialize(ViewEngineStartupContext viewEngineStartupContext)
{
}
internal Document ParseTemplate(TextReader template)
{
var parser = new Parser();
Document document;
var result = parser.Parse(template, _host, out document) ? document : null;
return result;
}
public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
{
return new HtmlResponse
{
Contents = stream =>
{
var document = ParseTemplate(viewLocationResult.Contents.Invoke());
var documentRenderer = _host.DependencyResolver.Get<DocumentRenderer>();
var result = documentRenderer.Render(document, model);
using (var writer = new StreamWriter(stream))
{
writer.Write(result);
}
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment