Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Denis 47 posts 68 karma points
    Feb 07, 2012 @ 11:28
    Denis
    0

    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

  • Dan Diplo 1463 posts 5630 karma points MVP 3x c-trib
    Feb 07, 2012 @ 11:36
    Dan Diplo
    0

    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.

  • Denis 47 posts 68 karma points
    Feb 07, 2012 @ 13:31
    Denis
    0

    One question, is it this metod slow?

    All pages must read settings, i think this traversing method can slow down web page ?

  • Dan Diplo 1463 posts 5630 karma points MVP 3x c-trib
    Feb 07, 2012 @ 14:20
    Dan Diplo
    0

    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));
        }
    }

     

  • Dan Diplo 1463 posts 5630 karma points MVP 3x c-trib
    Feb 07, 2012 @ 14:20
    Dan Diplo
    0

    I should point out I clear the cache in the page publish event, so it's never stale.

Please Sign in or register to post replies

Write your reply to:

Draft