Copied to clipboard

Flag this post as spam?

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


  • Jason 22 posts 41 karma points
    Jun 19, 2012 @ 07:20
    Jason
    0

    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!

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Jun 19, 2012 @ 10:14
    Michael Latouche
    0

    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:

    @Model._productHeaderImage

    Note that it is not an error that the first letter of the property is not capitalized :-)

    Hope this helps.

    Cheers,

    Michael.

  • Jason 22 posts 41 karma points
    Jun 26, 2012 @ 05:00
    Jason
    0

    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

     @{
                  var currentPage = Model;
                  while (currentPage.Level>3) { currentPage=currentPage.Parent; }
                  <ul>
                  @foreach (var c in currentPage.Children)
                  {
                   <li><href="@c.Url">@c.Name</a></li>
                   }
                  </ul>  
                
Please Sign in or register to post replies

Write your reply to:

Draft