我在我的站点中添加了一个全局站点区域,并在其中填充了一些内容。如何从页面视图和/或布局中读取此内容?
1 回答
This feature differs a bit between WebPages & MVC, the reason for this being that in WebPages (like WebForms) a Layout-page have a different model than the actual page being executed. If you use WebPages you simply add the following line first in the Layout page:
@inherits Piranha.WebPages.LayoutPage
This will automatically load the layout page model and all the global regions.
If you're using MVC this can't be done automatically as the Layout doesn't have a model. You can simply add the following in your Layout-page:
@{
Piranha.Models.PageModel global;
if (HttpContext.Current.Items["Piranha_CurrentPage"] != null) {
var current =
(Piranha.Models.Page)HttpContext.Current.Items["Piranha_CurrentPage"];
global = Piranha.Models.PageModel.GetBySite(current.SiteTreeId);
} else {
global = Piranha.Models.PageModel.GetBySite(Piranha.Config.SiteTreeId);
}
}
This snippet loads the layout page from:
- If it's a page is displayed it loads the site tree the page is contained in
- If it's a post it loads the site tree from the current site.
Hope this helps you!
Regards
/Håkan