Created
August 23, 2012 19:03
-
-
Save Buildstarted/3440372 to your computer and use it in GitHub Desktop.
Nancy implementation for the parrot view engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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