Has your Settings node got a unique doc type? If so, you can use it's alias to locate it (let's assume it's alias is "settings"):
var mySettings = Model.AncestorOrSelf().Descendants("settings").First();
int items = mySettings.newsItemsNumber;
Basically you "walk up" to the root node (Homepage) and then find the first descendant node with the alias of "settings". Just change "settings" to the actual alias of your Settings doc type.
The method isn't particularly slow, but it's obviously slower than using a "hard-coded" value. You can always add caching to your macros.
Another alternative (which is what I use) is to create a static class in /App_Code that retrives the settings node. My class looks like this:
public static class SiteSettings
{
/// <summary>
/// Gets a reference to the Site Settings node (used to store site-wide settings)
/// </summary>
/// <param name="model">The current model for the page</param>
/// <returns>A dynamic item that represents the Settings node</returns>
/// <remarks>This is cached to aid performance</remarks>
public static dynamic GetSettingsNodeFromModel(DynamicNode model)
{
dynamic node = HttpContext.Current.Cache.Get("Umbraco.SiteSettings.Node") as dynamic;
if (node == null)
{
// Use this for site with multiple home pages
//node = model.AncestorOrSelf().DescendantsOrSelf("SiteSettings").Items.FirstOrDefault();
// Use this for site where SiteSettings is outside home page
node = model.NodeById(-1).Descendants("SiteSettings").Items.FirstOrDefault();
if (node != null && !(node is DynamicNull))
{
HttpContext.Current.Cache.Insert("Umbraco.SiteSettings.Node", node);
}
}
return node;
}
/// <summary>
/// Gets a reference to the Site Settings node (used to store site-wide settings)
/// </summary>
/// <param name="model">The current nodeFactory node (eg. Node.GetCurrent())</param>
/// <returns>A dynamic item that represents the Settings node</returns>
/// <remarks>This is cached to aid performance</remarks>
public static dynamic GetSettingsNodeFromNode(Node node)
{
return GetSettingsNodeFromModel(new DynamicNode(node));
}
}
Accessing custom Settings page
I want to have my Settings page in Homepage node
Homepage
- About us
- News
- Contact
- Settings (has property newsItemsNumber ...)
How can i access newsItemsNumber property from News node ?
var mySettings = @Model.Parent.Settings; !?!?
@mySettings.newsItemsNumber
i see that maybe is possible somthing like NodeById, but is it possible get NodeByName ?
Thank You
Has your Settings node got a unique doc type? If so, you can use it's alias to locate it (let's assume it's alias is "settings"):
Basically you "walk up" to the root node (Homepage) and then find the first descendant node with the alias of "settings". Just change "settings" to the actual alias of your Settings doc type.
One question, is it this metod slow?
All pages must read settings, i think this traversing method can slow down web page ?
The method isn't particularly slow, but it's obviously slower than using a "hard-coded" value. You can always add caching to your macros.
Another alternative (which is what I use) is to create a static class in /App_Code that retrives the settings node. My class looks like this:
I should point out I clear the cache in the page publish event, so it's never stale.
is working on a reply...