Copied to clipboard

Flag this post as spam?

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


  • The Dude 4 posts 74 karma points
    Mar 28, 2016 @ 15:32
    The Dude
    0

    Partial View Based on Current Page

    Hi,

    I started yesterday, and so far, I have made a decient amount of progress in making my template. I have some annoyances, but mostly down to lack of knowledge or understanding and since I cannot find the solution on the forum, I thought I'd ask the experts.

    Within the header of my body, I have a banner area which I would like to have only when on the "home page", but cannot seem to call the banner at all. The current approach gives me a 404 error...

    To explain, what I am trying the achieve.

    On my master, within <body> <header> I have a reference @{ Html.RenderPartial("Banner"); } within this, I would like to use something like (for example) @if (DocumentTypeAlias == "Home") {CSS and @CurrentPage.Banner stuff} and if the current page is not the home page, then this partial view should be skipped.

    I hope this makes sense...

    Can someone give me any idea how to approach/achive this, I am struggling a tad.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Mar 28, 2016 @ 19:34
    Marc Goodson
    0

    Hi The Dude

    A few approaches here, you could as you suggest check for the document type alias in your master layout view eg:

      @if (Model.Content.DocumentTypeAlias == "Home")
                            {
                                <div class="banner-holder">
                                   @{ Html.RenderPartial("Banner");}
                                         </div>
                            }
    
                            }
                            else
                            {
                                <!-- no banner thing -->
                            }
    

    or you could define a section...

    In your master view, you can define a section like so:

     @RenderSection("BannerSection", false)
    

    (false here means the section is not required on any views inheriting the master layout view)

    Then in your homepage template, you can specify what you want to appear in this section like so:

    @section BannerSection {
        <h2>Hello Banner</h2>
     @{ Html.RenderPartial("Banner");}
    }
    

    what you put in this section in the homepage view, will appear in the position in the master view where you have put @RenderSection, if that makes sense ?

    leaving out the @section on other templates means nothing will be displayed here.

    Another approach similar to the first, would be to have the banner be some kind of content picker property, so the banner could be picked on other page layouts; and then use whether or not this picker has something picked to decide whether or not to render any html in the Banner Render Partial...

    @if (Model.Content.HasProperty("bannerPicker") && Model.Content.HasValue("bannerPicker"))
    {
        bannerContentId = Model.Content.GetPropertyValue<int>("bannerPicker");
        bannerContent = Umbraco.TypedContent(bannerContentId);
        <h2>@bannerContent.Name</h2>
        <!-- Render the banner -->
    }
    else
    {
        <!-- don't render the banner -->
    }
    
  • The Dude 4 posts 74 karma points
    Mar 28, 2016 @ 19:51
    The Dude
    0

    Hi, thanks very much, it would appear I was simply missing the model content. It works a charm.

    However, although it is now working with option 1, I like your banner picker approach better, the option of enabling and disabling based on a check box on a page is a more logical approach going forward.

    One thing however, in my CSS I have defined how the banner will sit. However once it is loaded in the browser, the grid is being loaded with some additional code which is messing stuff up. Do you know where these grid classes are defined, so I can remove them?

    <div class="umb-grid">
    <div class="grid-section">
    

    Thanks again.

  • The Dude 4 posts 74 karma points
    Mar 28, 2016 @ 20:09
    The Dude
    0

    No matter. Found it under grids :)

Please Sign in or register to post replies

Write your reply to:

Draft