示例#1
0
        public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
        {
            //we already have a view but we still need a view provider
            return new HtmlResponse(contents: stream =>
                {
                    var host = new NancyParrotHost(_modelValueProviderFactory, new NancyPathResolver(renderContext));
                    var parrotWriter = host.CreateWriter();
                    var rendererFactory = host.RendererFactory;
                    var view = new ParrotView(host, rendererFactory, _parrotViewLocator, viewLocationResult.Contents, parrotWriter);
                    var writer = new StreamWriter(stream);
                    
                    view.Render(model, writer);

                    writer.Flush();
                });
        }
示例#2
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            string layout = "";
            if (statement.Parameters != null && statement.Parameters.Any())
            {
                Type modelType = model != null ? model.GetType() : null;
                var modelValueProvider = Host.ModelValueProviderFactory.Get(modelType);

                //var parameterLayout = GetLocalModelValue(documentHost, statement, modelValueProvider, model) ?? "_layout";
                var parameterLayout = GetLocalModel(documentHost, statement, model) ?? "_layout";

                //assume only the first is the path
                //second is the argument (model)
                layout = parameterLayout.ToString();
            }


            //ok...we need to load the view
            //then pass the model to it and
            //then return the result

            var result = _parrotViewLocator.LocateView(layout);

            if (result != null)
            {
                var parrotView = new ParrotView(_host, rendererFactory, _parrotViewLocator, result.Contents, writer);
                string contents = result.Contents().ReadToEnd();

                var document = parrotView.LoadDocument(contents);

                //Create a new DocumentView and DocumentHost
                if (!documentHost.ContainsKey("_LayoutChildren_"))
                {
                    documentHost.Add("_LayoutChildren_", new Queue<StatementList>());
                }
                (documentHost["_LayoutChildren_"] as Queue<StatementList>).Enqueue(statement.Children);

                DocumentView view = new DocumentView(_host, rendererFactory, documentHost, document);

                view.Render(writer);
            }
            else
            {
                throw new Exception(string.Format("Layout {0} could not be found", layout));
            }
        }