I am probably missing something obvious, but is there any way of having some common Razor that I can share between templates.
For example, every page has properties pageTitle and bannerImage : I would like some common razor that I can have on each template without having to paste it into each one eg
@{
var pageTitle = Umbraco.Field("PageTitle");
var bannerImageId = Int32.Parse("0" + Model.Content.GetPropertyValue("bannerImage").ToString());
var bannerImageUrl = "";
if(bannerImageId != 0){
bannerImageUrl = Umbraco.Media(bannerImageId).Url;
}
I can't put it into the parent template as variables don't seem to be inherited by child templates(?) nor from partial views.
Is there any way of sharing Razor snippets to allow central storing of common razor?
Then I would get the partial code to get the values and store them in the cache or session. Then it can check for them first and get them if they have not been set.
Shared Razor
I am probably missing something obvious, but is there any way of having some common Razor that I can share between templates.
For example, every page has properties pageTitle and bannerImage : I would like some common razor that I can have on each template without having to paste it into each one eg
I can't put it into the parent template as variables don't seem to be inherited by child templates(?) nor from partial views.
Is there any way of sharing Razor snippets to allow central storing of common razor?
This is just C# - it doesn't render anything. I would create an extension method of IPublishedContent. Then you could do something like:
I've seen something about creating functions in the App_Code folder. So you could check that out, but it's not something I've done myself.
If every page has these properties (as you mentioned) then perhaps a simple solution would be to set them within the _Layout.cshtml?
I'm pretty sure that your page views or partials can access the layout, but by all means try it. Let me know if you can!
That should have said 'can't access'.
I would have the common razor in a partial and call it from the template like this
@{ Html.RenderPartial("_Header"); }
If the common razor is rendering HTML and not computational then I absolutely agree.
I am trying to use it to set c# variables, so I don't believe a partial will do as variables are local to the partial, I think.
Then I would get the partial code to get the values and store them in the cache or session. Then it can check for them first and get them if they have not been set.
Yes, adding variables to the request cache would work, but I still think an extension method is the way to go.
is working on a reply...