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.
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?
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?
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?
Hi Dimitry,
what I always do is create an extension class on the UmbracoHelper like so:
Using this, al of your queries are located in one file and then on the partials you can do:
Hope this helps.
/Michaël
Hi, Michaël
I did't get what your code do :(
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 :)
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
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?
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
Exactly! :)
Yes, It is an another way, but it forces to pass a parameter and use strongly typed view like
But, I want to create something like cozy API for common tasks and make partial views independent of parameters.
Thanks, Michaël
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.
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!
is working on a reply...