func getBlogContext(reqId uint, embed *service.EmbedNode, s *service.Session, settings *settings, renderer *mtemplate.Renderer) ( map[string][]byte, *service.CacheMods, error) { req, err := s.Monsti().GetRequest(reqId) if err != nil { return nil, nil, fmt.Errorf("Could not get request: %v", err) } query := req.Query blogPath := req.NodePath if embed != nil { embedUrl, err := url.Parse(embed.URI) if err != nil { return nil, nil, fmt.Errorf("Could not parse embed URI") } query = embedUrl.Query() blogPath = embedUrl.Path } limit := -1 if limitParam, err := strconv.Atoi(query.Get("limit")); err == nil { limit = limitParam if limit < 1 { limit = 1 } } context := mtemplate.Context{} context["Embedded"] = embed context["Posts"], err = getBlogPosts(req, blogPath, s, limit) if err != nil { return nil, nil, fmt.Errorf("Could not retrieve blog posts: %v", err) } rendered, err := renderer.Render("core/blogpost-list", context, req.Session.Locale, settings.Monsti.GetSiteTemplatesPath(req.Site)) if err != nil { return nil, nil, fmt.Errorf("Could not render template: %v", err) } mods := &service.CacheMods{ Deps: []service.CacheDep{{Node: req.NodePath, Descend: 1}}, } return map[string][]byte{"BlogPosts": rendered}, mods, nil }
// renderInMaster renders the content in the master template. func renderInMaster(r template.Renderer, content []byte, env masterTmplEnv, settings *settings, site string, siteSettings *service.Settings, userLocale string, s *service.Session) ([]byte, *service.CacheMods) { mods := &service.CacheMods{Deps: []service.CacheDep{{Node: "/", Descend: -1}}} if env.Flags&EDIT_VIEW != 0 { ret, err := r.Render("admin/master", template.Context{ "Site": site, "SiteSettings": siteSettings, "Page": template.Context{ "Title": env.Title, "Node": env.Node, "EditView": true, "SlimView": env.Flags&SLIM_VIEW != 0, "Content": htmlT.HTML(content), }, "Session": env.Session}, userLocale, settings.Monsti.GetSiteTemplatesPath(site)) if err != nil { panic("Can't render admin template: " + err.Error()) } return ret, nil } firstDir := splitFirstDir(env.Node.Path) getNodeFn := func(path string) (*service.Node, error) { node, err := s.Monsti().GetNode(site, path) return node, err } getChildrenFn := func(path string) ([]*service.Node, error) { return s.Monsti().GetChildren(site, path) } priNavDepth := 1 if nav, ok := siteSettings.Fields["core.Navigations"].(*service.MapField). Fields["core.Main"]; ok { priNavDepth = nav.(*service.CombinedField). Fields["depth"].Value().(int) } prinav, err := getNav("/", path.Join("/", firstDir), env.Session.User == nil, getNodeFn, getChildrenFn, priNavDepth) if err != nil { panic(fmt.Sprint("Could not get primary navigation: ", err)) } prinav.MakeAbsolute("/") var secnav navigation = nil if env.Node.Path != "/" { secnav, err = getNav(env.Node.Path, env.Node.Path, env.Session.User == nil, getNodeFn, getChildrenFn, 1) if err != nil { panic(fmt.Sprint("Could not get secondary navigation: ", err)) } secnav.MakeAbsolute(env.Node.Path) } blocks := make(map[string][]renderedBlock) // EXPERIMENTAL Render blocks if _, ok := siteSettings.Fields["core.RegionBlocks"].(*service.MapField). Fields["core.PrimaryNavigation"].(*service.ListField); ok { renderedNav, err := r.Render("blocks/core/Navigation", template.Context{ "Id": "core.PrimaryNavigation", "Links": prinav, }, userLocale, settings.Monsti.GetSiteTemplatesPath(site)) if err != nil { panic(fmt.Sprintf("Could not render navigation: %v", err)) } blocks["core.PrimaryNavigation"] = append(blocks["core.PrimaryNavigation"], renderedBlock{ Rendered: htmlT.HTML(renderedNav), }) } title := getNodeTitle(env.Node) ret, err := r.Render("master", template.Context{ "Site": site, "SiteSettings": siteSettings, "Page": template.Context{ "Node": env.Node, "PrimaryNav": prinav, // TODO DEPRECATED "SecondaryNav": secnav, // TODO DEPRECATED "EditView": env.Flags&EDIT_VIEW != 0, "Title": title, "Content": htmlT.HTML(content), "ShowSecondaryNav": len(secnav) > 0, // TODO DEPRECATED "Blocks": blocks, }, "Session": env.Session}, userLocale, settings.Monsti.GetSiteTemplatesPath(site)) if err != nil { panic(fmt.Sprintf( "Can't render master template for site %v: %v", site, err)) } return ret, mods }
// renderInMaster renders the content in the master template. func renderInMaster(r template.Renderer, content []byte, env masterTmplEnv, settings *settings, site util.SiteSettings, locale string, s *service.Session) string { if env.Flags&EDIT_VIEW != 0 { ret, err := r.Render("admin/master", template.Context{ "Site": site, "Page": template.Context{ "Title": env.Title, "Node": env.Node, "EditView": env.Flags&EDIT_VIEW != 0, "Content": htmlT.HTML(content), }, "Session": env.Session}, locale, settings.Monsti.GetSiteTemplatesPath(site.Name)) if err != nil { panic("Can't render: " + err.Error()) } return ret } firstDir := splitFirstDir(env.Node.Path) getNodeFn := func(path string) (*service.Node, error) { node, err := s.Monsti().GetNode(site.Name, path) return node, err } getChildrenFn := func(path string) ([]*service.Node, error) { return s.Monsti().GetChildren(site.Name, path) } prinav, err := getNav("/", path.Join("/", firstDir), env.Session.User == nil, getNodeFn, getChildrenFn) if err != nil { panic(fmt.Sprint("Could not get primary navigation: ", err)) } prinav.MakeAbsolute("/") var secnav navigation = nil if env.Node.Path != "/" { secnav, err = getNav(env.Node.Path, env.Node.Path, env.Session.User == nil, getNodeFn, getChildrenFn) if err != nil { panic(fmt.Sprint("Could not get secondary navigation: ", err)) } secnav.MakeAbsolute(env.Node.Path) } title := getNodeTitle(env.Node) ret, err := r.Render("master", template.Context{ "Site": site, "Page": template.Context{ "Node": env.Node, "PrimaryNav": prinav, "SecondaryNav": secnav, "EditView": env.Flags&EDIT_VIEW != 0, "Title": title, "Content": htmlT.HTML(content), "ShowSecondaryNav": len(secnav) > 0}, "Session": env.Session}, locale, settings.Monsti.GetSiteTemplatesPath(site.Name)) if err != nil { panic("Can't render: " + err.Error()) } return ret }