Esempio n. 1
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            //get the parameter
            var partial = GetLocalModel(documentHost, statement, model) as string;
            if (string.IsNullOrEmpty(partial))
            {
                throw new ArgumentNullException("partial");
            }
            //ok...we need to load the layoutpage
            //then pass the node's children into the layout page
            //then return the result
            var engine = (Host as AspNetHost).ViewEngine;
            var result = engine.FindView(null, partial, null, false);
            if (result != null)
            {
                var parrotView = (result.View as ParrotView);
                using (var stream = parrotView.LoadStream())
                {
                    var document = parrotView.LoadDocument(stream);

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

                    view.Render(writer);
                }
            }
        }
Esempio n. 2
0
        private string Parse(object model, string template)
        {
            var document = LoadDocument(template);
            var documentHost = CreateDocumentHost(model);
            
            var view = new DocumentView(_host, _rendererFactory, documentHost, document);

            view.Render(_writer);

            return _writer.Result();
        }
Esempio n. 3
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));
            }
        }
Esempio n. 4
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 = 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 engine = (Host as AspNetHost).ViewEngine;
            var result = engine.FindView(null, layout, null, false);
            if (result != null)
            {
                var parrotView = (result.View as ParrotView);
                using (var stream = parrotView.LoadStream())
                {
                    var document = parrotView.LoadDocument(stream);

                    //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);
                }
            }
        }
Esempio n. 5
0
        private string Parse(ViewContext viewContext, Stream template)
        {
            Stopwatch watch = Stopwatch.StartNew();

            Document document = LoadDocument(template);

            if (document.Errors.Any())
            {
                throw new Exception(string.Join("\r\n", document.Errors));
            }

            object model = null;
            if (viewContext != null)
            {
                model = viewContext.ViewData.Model;
            }

            var documentHost = new Dictionary<string, object>();
            documentHost.Add(DocumentView.ModelName, model);
            if (viewContext != null)
            {
                documentHost.Add(RequestName, viewContext.RequestContext.HttpContext.Request);
                documentHost.Add(UserName, viewContext.RequestContext.HttpContext.User);
                documentHost.Add(ViewContextName, viewContext);
                documentHost.Add(ControllerContextName, viewContext.Controller.ControllerContext);
                documentHost.Add(ViewBagName, viewContext.ViewData);
            }

            //need to create a custom viewhost
            var rendererFactory = _host.RendererFactory;
            DocumentView view = new DocumentView(_host, rendererFactory, documentHost, document);

            IParrotWriter writer = _host.CreateWriter();
            view.Render(writer);

            watch.Stop();

            return writer.Result();
            //+ "\r\n\r\n<!--\r\n" + template + "\r\n-->"
            //+ "\r\n\r\n<!--\r\n" + watch.Elapsed + "\r\n-->";
        }