Razor return root image from nested template (master page)
Currently have following hierarchy
Master Page
---------Product Page (nested master)
-----------------Product Sub Page (nested master)
Product Page has a property header image defined in it's document type. What I want is to always show the Product Page header image for any sub page. I'm currently accomplishing this using @Model.Parent but am looking to see if this is the best practice way to navigate up. What if there were multiple nested sub pages would I have to manually traverse the tree up to the Product Page. Anyway below is the code that is working but wanted to get thoughts.
Another option might be to use the "recursive" property, so that it would get the property from the node if it is present, or else look for the first parent having that property. This way you can avoid the nested if's.
The syntax is then:
@Model._productHeaderImage
Note that it is not an error that the first letter of the property is not capitalized :-)
Razor return root image from nested template (master page)
Currently have following hierarchy
Master Page
---------Product Page (nested master)
-----------------Product Sub Page (nested master)
Product Page has a property header image defined in it's document type. What I want is to always show the Product Page header image for any sub page. I'm currently accomplishing this using @Model.Parent but am looking to see if this is the best practice way to navigate up. What if there were multiple nested sub pages would I have to manually traverse the tree up to the Product Page. Anyway below is the code that is working but wanted to get thoughts.
<umbraco:Macro runat="server" language="cshtml">
@if (Model.HasValue("ProductHeaderImage"))
{
<img src="@Model.productHeaderImage.mediaItem.Image.umbracoFile" style="padding-bottom:15px;" />
}
else
{
if(@Model.Parent.HasValue("ProductHeaderImage"))
{
<img src="@Model.Parent.productHeaderImage.mediaItem.Image.umbracoFile" style="padding-bottom:15px;" />
}
else
{
<img src="/images/generic-header.jpg" style="padding-bottom:15px;" />
}
}
</umbraco:Macro>
Thanks!
Hi Jason,
Another option might be to use the "recursive" property, so that it would get the property from the node if it is present, or else look for the first parent having that property. This way you can avoid the nested if's.
The syntax is then:
Note that it is not an error that the first letter of the property is not capitalized :-)
Hope this helps.
Cheers,
Michael.
Hi Michael,
Thanks for the response. That will do the trick. I've also seen where using .Level is another way if you know the exact depths.
Thanks again,
Jason
is working on a reply...