コード例 #1
0
ファイル: TemplatesModule.cs プロジェクト: synhershko/NSemble
        public TemplatesModule(IDocumentSession session, IViewLocator viewLocator)
            : base("Templates")
        {
            Get["/"] = p =>
                           {
                               var templates = session.Advanced.LoadStartingWith<ViewTemplate>("NSemble/Views/");
                               return View["List", templates];
                           };

            Get["/new/"] = p => View["Edit", new ViewTemplate {}];

            Get[@"/edit/{viewName*}"] = p =>
                                            {
                                                var viewName = (string) p.viewName;
                                                if (!viewName.StartsWith(Constants.RavenViewDocumentPrefix, StringComparison.InvariantCultureIgnoreCase))
                                                    viewName = Constants.RavenViewDocumentPrefix + viewName;
                                                var template = session.Load<ViewTemplate>(viewName);

                                                // Even if we don't have it stored in the DB, it might still exist as a resource. Try loading it from Nancy.
                                                if (template == null)
                                                {
                                                    var vlr = viewLocator.LocateView(viewName.Substring(Constants.RavenViewDocumentPrefix.Length), Context);
                                                    if (vlr == null)
                                                        return 404;

                                                    template = new ViewTemplate
                                                                   {
                                                                       Location = vlr.Location,
                                                                       Name = vlr.Name,
                                                                       Extension = vlr.Extension,
                                                                       Contents = vlr.Contents.Invoke().ReadToEnd(),
                                                                   };
                                                }

                                                return View["Edit", template];
                                            };

            Post[@"/edit/{viewName*}"] = p =>
                                                   {
                                                       var template = this.Bind<ViewTemplate>();

                                                       var viewName = (string) p.viewName;
                                                       if (!viewName.StartsWith(Constants.RavenViewDocumentPrefix, StringComparison.InvariantCultureIgnoreCase))
                                                           viewName = Constants.RavenViewDocumentPrefix + viewName;

                                                       session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));
                                                       session.SaveChanges();

                                                       return "Success";
                                                   };

            Post["/new"] = p =>
                                {
                                    var template = this.Bind<ViewTemplate>();
                                    session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));

                                    return Response.AsRedirect("/");
                                };
        }
コード例 #2
0
        public TemplatesModule(IDocumentSession session, IViewLocator viewLocator)
            : base("Templates")
        {
            Get["/"] = p =>
            {
                var templates = session.Query <ViewTemplate>().ToArray();

                return(View["List", templates]);
            };

            Get["/new/"] = p => View["Edit", new ViewTemplate {
                                     }];

            Get[@"/edit/(?<viewName>\S+)/"] = p =>
            {
                var viewName = (string)p.viewName;
                var template = session.Load <ViewTemplate>(Constants.RavenViewDocumentPrefix + viewName);

                // Even if we don't have it stored in the DB, it might still exist as a resource. Try loading it from Nancy.
                if (template == null)
                {
                    ViewLocationResult vlr = viewLocator.LocateView(viewName, Context);
                    if (vlr == null)
                    {
                        return(404);
                    }

                    template = new ViewTemplate
                    {
                        Location  = vlr.Location,
                        Name      = vlr.Name,
                        Extension = vlr.Extension,
                        Contents  = vlr.Contents.Invoke().ReadToEnd(),
                    };
                }

                return(View["Edit", template]);
            };

            Post[@"/edit/(?<viewName>\S+)/"] = p =>
            {
                var template = this.Bind <ViewTemplate>();
                var viewName = (string)p.viewName;

                session.Store(template, Constants.RavenViewDocumentPrefix + viewName);
                session.SaveChanges();

                return("Success");
            };

            Post["/new/"] = p =>
            {
                var template = this.Bind <ViewTemplate>();
                session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));

                return(Response.AsRedirect("/"));
            };
        }
コード例 #3
0
ファイル: TemplatesModule.cs プロジェクト: jchannon/NSemble
        public TemplatesModule(IDocumentSession session, IViewLocator viewLocator)
            : base("Templates")
        {
            Get["/"] = p =>
                           {
                               var templates = session.Query<ViewTemplate>().ToArray();

                               return View["List", templates];
                           };

            Get["/new/"] = p => View["Edit", new ViewTemplate {}];

            Get[@"/edit/(?<viewName>\S+)/"] = p =>
                                                  {
                                                      var viewName = (string) p.viewName;
                                                var template = session.Load<ViewTemplate>(viewName);

                                                // Even if we don't have it stored in the DB, it might still exist as a resource. Try loading it from Nancy.
                                                if (template == null)
                                                {
                                                    ViewLocationResult vlr = viewLocator.LocateView(viewName, Context);
                                                    if (vlr == null)
                                                        return 404;

                                                    template = new ViewTemplate
                                                                   {
                                                                       Location = vlr.Location,
                                                                       Name = vlr.Name,
                                                                       Extension = vlr.Extension,
                                                                       Contents = vlr.Contents.Invoke().ReadToEnd(),
                                                                   };
                                                }

                                                return View["Edit", template];
                                            };

            Post["/new/"] = p =>
                                {
                                    var template = this.Bind<ViewTemplate>();
                                    session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));

                                    return Response.AsRedirect("/");
                                };
        }
コード例 #4
0
        /// <summary>
        /// Finds the specified view by using the specified controller context.
        /// </summary>
        /// <returns>
        /// The page view.
        /// </returns>
        /// <param name="controllerContext">The controller context.</param><param name="viewName">The name of the view.</param><param name="masterName">The name of the master.</param><param name="useCache">true to specify that the view engine returns the cached view, if a cached view exists; otherwise, false.</param>
        public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            Template.FileSystem = _fileSystem.GetFileSystem(this.Extensions);
            var view = _viewLocator.LocatePartialView(viewName);

            if (String.IsNullOrEmpty(masterName))
            {
                masterName = "theme";
            }

            var master = _viewLocator.LocateView(masterName);

            if (view.SearchedLocations != null)
            {
                return(new ViewEngineResult(view.SearchedLocations));
            }

            return(new ViewEngineResult(new DotLiquidView(controllerContext, _viewLocator, view, master), this));
        }
コード例 #5
0
        public void Render(ViewContext viewContext, TextWriter writer)
        {
            if (viewContext == null)
            {
                throw new ArgumentNullException("viewContext");
            }

            // Copy data from the view context over to DotLiquid
            var localVars = new Hash();

            if (viewContext.ViewData.Model != null)
            {
                var model = viewContext.ViewData.Model;
                if (model is ILiquidizable)
                {
                    model = ((ILiquidizable)model).ToLiquid();
                }

                if (model is Hash)
                {
                    localVars = model as Hash;
                }
                else if (model is IDictionary <string, object> )
                {
                    var modelDictionary = model as IDictionary <string, object>;
                    foreach (var item in modelDictionary.Keys)
                    {
                        localVars.Add(item, modelDictionary[item]);
                    }
                }
            }

            foreach (var item in viewContext.ViewData)
            {
                localVars.Add(Template.NamingConvention.GetMemberName(item.Key), item.Value);
            }

            foreach (var item in viewContext.TempData)
            {
                localVars.Add(Template.NamingConvention.GetMemberName(item.Key), item.Value);
            }

            var renderParams = new RenderParameters {
                LocalVariables = Hash.FromDictionary(localVars)
            };

            var template = GetTemplateFromFile(this.ViewResult);

            if (this.MasterViewResult == null)
            {
                template.RenderWithTracing(writer, renderParams);
            }
            else // add master
            {
                var renderedContents = template.RenderWithTracing(renderParams);

                // read layout from context
                var layout = template.Registers["layout"].ToNullOrString();

                var layoutPath = layout == null
                    ? this.MasterViewResult
                    : _locator.LocateView(layout);

                // render master with contents
                var masterTemplate         = GetTemplateFromFile(layoutPath);
                var headerTemplate         = GetTemplateFromFile(_locator.LocatePartialView("content_header"));
                var renderedHeaderContents = headerTemplate.RenderWithTracing(renderParams);

                renderParams.LocalVariables.Add("content_for_layout", renderedContents);
                renderParams.LocalVariables.Add("content_for_header", renderedHeaderContents);
                masterTemplate.RenderWithTracing(writer, renderParams);
            }
        }