Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 168 posts 588 karma points
    Apr 25, 2018 @ 14:22
    Dmitriy
    0

    Share root node of site tree within partial views and nested layouts?

    Hello, Umbracians.

    I often use lot of partials views on the same page (root page), but often it forces me to create redundant queries to root node of site tree over and over again to get data from subnodes.

    So, what is the question? How to share query result within partial views and nested layouts?

    Ofcourse I can just use ViewBag on master layout and get from there next, but I think it is not a reliable way.

    How would you deal with it?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 25, 2018 @ 14:49
    Michaël Vanbrabandt
    0

    Hi Dimitry,

    what I always do is create an extension class on the UmbracoHelper like so:

        public static IPublishedContent ContentRepository(this UmbracoHelper helper)
        {
            return helper.TypedContentSingleAtXPath("//ucContentRepositories");
        }
    

    Using this, al of your queries are located in one file and then on the partials you can do:

    var contentNode = Umbraco.ContentRepository();
    

    Hope this helps.

    /Michaël

  • Dmitriy 168 posts 588 karma points
    Apr 25, 2018 @ 15:29
    Dmitriy
    0

    Hi, Michaël

    I did't get what your code do :(

    // What do you get here?
    return helper.TypedContentSingleAtXPath("//ucContentRepositories");  
    

    What XPath here for? What does it do? What role of XML here? Where is the data source of XPath query?

    Im brand new with XPath :)

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 26, 2018 @ 06:49
    Michaël Vanbrabandt
    1

    Hi Dimitry,

    this is just an example method, the purpose was to show you how you can create an extension class where you put your queries in.

    In this way you only have one place to put your queries and you can access them from every page or partial view without rewriting the query.

    Hope this helps.

    /Michaël

  • Dmitriy 168 posts 588 karma points
    Apr 26, 2018 @ 08:20
    Dmitriy
    0
    In this way you only have one place to put your queries and you can access them from every page or partial view without rewriting the query.
    

    Oh, now I get it. But this is not what I meant. The problem not with need to store the query itself. I need to store the query result for not to execute query over and over again on every partial view.

    But, I think I know what you are goind to say. Do you mean it need to use a static class (extensions are static as we know) because it have only one instance. Am I right?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 26, 2018 @ 08:23
    Michaël Vanbrabandt
    1

    Aaaaah ok, mis understood then.

    Well if you only want to run the query once and pass it into your partials, why not executing it on the page itself where you call all the partials and pass it into each of them where you need it?

    Hope this helps.

    /Michaël

  • Dmitriy 168 posts 588 karma points
    Apr 26, 2018 @ 08:40
    Dmitriy
    0

    Well if you only want to run the query once and pass it into your partials

    Exactly! :)

    why not executing it on the page itself where you call all the partials and pass it into each of them where you need it?

    Yes, It is an another way, but it forces to pass a parameter and use strongly typed view like

    @Html.Partial("PartialName", queryResult )
    

    But, I want to create something like cozy API for common tasks and make partial views independent of parameters.

    Thanks, Michaël

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Apr 26, 2018 @ 06:02
    Paul Seal
    1

    I usually just do this at the top of my partial.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{ IPublishedContent homePage = Umbraco.AssignedContentItem.AncestorOrSelf("Home"); }

    I'm on mobile so please forgive the formatting.

  • Dmitriy 168 posts 588 karma points
    Apr 26, 2018 @ 08:58
    Dmitriy
    0

    Hi, Paul

    But It means you are do the same work, if you've got the HomePage before (for main navigation for example), or that method is very cheap for query?

    Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft