Best way to make settings node values available in views
Hi,
I have the below structure where I have two content nodes containing different data to be used across a lot of view files.
Right now I have added this code in every view file where I need to access these data.
var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf().OfTypes("site_folder_settings").FirstOrDefault();
var settingsAdmin = settingsFolder.DescendantsOrSelf().OfTypes("site_settings_admin").FirstOrDefault();
var settingsEditor = settingsFolder.DescendantsOrSelf().OfTypes("site_settings_editor").FirstOrDefault();
And then I fetch values this way:
<div>@settingsEditor.Value("footerCol1")</div>
That is both redundant and probably also waste of ressources to do the same queries multiple times.
I am looking for a way to do the queries once in an *.cs file in App_Code where I then can call them from view files. But my skills are not enough to that now. Can anyone give me a hint?
var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault().
DescendantsOrSelf().OfTypes("site_folder_settings").FirstOrDefault();
to your suggestion:
var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault()
.Children.FirstOrDefault(x => x.ContentType.Alias.Equals("site_folder_settings"));
Then settingsFolder suddenly becomes null (which is also a surprise to me).
Regarding bullet 1: Caching sounds like a good idea. But what about all the other content of the view that I cache? Won´t everything be cached (which I do not want)?
Following my reply on the FB Umbraco Group on the same question, I'm posting this also here hoping that it can be helpful for anyone investigating approaches to caching "special" nodes:
I use a "cached nodes" approach where I cache some nodes (usually unique ones, like settings etc) in the runtime cache (clearing it whenever something is published) and then using this wherever it's needed. Goes hand in hand with modelsbuilder.
is that v8 Martin? What I did is create an umbraco helper extension that you can use for easily accessing these setting nodes without having the login in your views...
you can then do stuff like @Umbraco.WebSiteSettings.Footer
We have been discussing Sotiris work on the slack channel and it feels a bit strange to cache cached content, will probably result in unwanted effects..
I've updated the post with all the useful parts from our discussion in Slack, providing possible answers and alternative ways to implement this. Hope it'll be good as a starting point for anyone who reads it and needs to decide how to implement something similar.
Thanks. I will maybe try this at a later point. For now I can see that I lack some bit of knowledge into .NET i general and Umbraco Models Builder in particular.
So for now I will put my site settings on the home node an use fallback: Fallback.ToAncestors
This is ok for me since for the particular site I am building editors are allowed to see and edit them.
Best way to make settings node values available in views
Hi,
I have the below structure where I have two content nodes containing different data to be used across a lot of view files.
Right now I have added this code in every view file where I need to access these data.
And then I fetch values this way:
That is both redundant and probably also waste of ressources to do the same queries multiple times.
I am looking for a way to do the queries once in an *.cs file in App_Code where I then can call them from view files. But my skills are not enough to that now. Can anyone give me a hint?
Hi Martin,
There are few ways to optimize queries like that.
1) You can just cache the view where this code is placed
2) As I see you don't need descendants method, which one is little bit heavy, try to use Children instead:
Please replace "global_doc" with Global folder doctype alias.
Thanks,
Alex
Hi Alex,
Thanks for your comments.
Regarding bullet 2: If I change this:
to your suggestion:
Then settingsFolder suddenly becomes null (which is also a surprise to me).
Regarding bullet 1: Caching sounds like a good idea. But what about all the other content of the view that I cache? Won´t everything be cached (which I do not want)?
In FB-forum "Umbraco Web Developers" (https://www.facebook.com/groups/202933450108554) I have been pointed to a similar solution that is the one I will now test: https://www.dot-see.com/en/blog/caching-umbraco-nodes-for-use-in-multiple-places/
In this code snippen you miss "Global" node I think
I don´t think so. It has the same "strucure" as the working code (only ".DescendantsOrSelf()" has been replaced with ".Children").
Following my reply on the FB Umbraco Group on the same question, I'm posting this also here hoping that it can be helpful for anyone investigating approaches to caching "special" nodes:
I use a "cached nodes" approach where I cache some nodes (usually unique ones, like settings etc) in the runtime cache (clearing it whenever something is published) and then using this wherever it's needed. Goes hand in hand with modelsbuilder.
Blog post (and embedded gist) here: https://www.dot-see.com/en/blog/caching-umbraco-nodes-for-use-in-multiple-places/
Thanks, Sotiris. I mark your answer as a solution. I haven´t tested it yet, but it seems like the approach I looked for.
For now my .NET and Umbraco skills are not sufficient to implement your solution, but I will get there. :)
is that v8 Martin? What I did is create an umbraco helper extension that you can use for easily accessing these setting nodes without having the login in your views...
you can then do stuff like @Umbraco.WebSiteSettings.Footer
We have been discussing Sotiris work on the slack channel and it feels a bit strange to cache cached content, will probably result in unwanted effects..
I've updated the post with all the useful parts from our discussion in Slack, providing possible answers and alternative ways to implement this. Hope it'll be good as a starting point for anyone who reads it and needs to decide how to implement something similar.
Cool! 💪
Thanks for the info.
Yes, it´s v8. Have you got an example or some kind of directions you can point me?
here you go https://dev.to/timgeyssens/extending-the-umbracohelper-class-in-umbraco-v8-3l0l
Thanks. I will maybe try this at a later point. For now I can see that I lack some bit of knowledge into .NET i general and Umbraco Models Builder in particular.
So for now I will put my site settings on the home node an use fallback: Fallback.ToAncestors
This is ok for me since for the particular site I am building editors are allowed to see and edit them.
Yes, just writing a post hold on :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.