Copied to clipboard

Flag this post as spam?

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


  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 26, 2015 @ 14:52
    Fuji Kusaka
    0

    Nested Templates

    Hi Guys,

    Am getting some issue with a nested template with and would like to some help on nested razor views.

    I have the following templates structure - Master (Templates) -- Page (Template) --- Page Item --- Page Item 2

    Now in my Master i already added @RenderBody() and using @RenderSection("header", required: false)

    In Page Template @Section header{ @currentPage.Name }

    However i need to Apply Page Item Template to some other page node content and did something like @RenderBody() @RenderSection("others", required:false) and ended up adding some more codes in the Page Item (template)

    @section others{ //some text or code }

    But where the Page Template is used am getting this error : " cannot be requested directly because it calls the "RenderBody" method."

    Any advise what i might be doing wrong here ?

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 26, 2015 @ 15:01
    Steve Morgan
    0

    Hi,

    Sorry not sure I understand your problem!? Are you struggling to get the child template to use the master.

    What error do you get? In the most basic form your child template Page (Template) should have something like:

      @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @{
                Layout = "Master.cshtml";
            }
        <div class="childcontent">
        <h1>your child content</h1>
        </div>
    

    You only use the RenderSection for content in areas outside the "main" child content (usually the div with id=container etc).

    Steve

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 26, 2015 @ 15:16
    Fuji Kusaka
    0

    Hi Steve,

    Sorry my formatting was not good

    I actually have this template structure

    - Master 
    -- Template 1
    --- Sub Template 1
    --- Sub Template 2
    

    I need to Apply Sub Template 1 on some node but its returning me this error on pages where Template 1 is used

    cannot be requested directly because it calls the "RenderBody" method.
    

    This is how my Master looks like

    <Body>
    @RenderBody()
    @RenderSection("pgTxt", required:false)
    

    Template 1

    @section pgTxt{
    // some text and fields
    Renderbody()
    RenderSection("footer", required:false)
    }
    

    Sub Template 1

    @section footer{
    }
    

    By the way am converting an old umbraco to mvc

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 26, 2015 @ 15:24
    Steve Morgan
    0

    You can only have one Render Body.. I'm not sure what you're trying to achieve with the multiple levels of inheritance but I'd only ever have one master and a child.

    This has been asked before with some good links from Dennis explaining why.

    https://our.umbraco.org/forum/developers/razor/56787-Nested-layout-pages-and-RenderBody

    HTH

    Steve

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 26, 2015 @ 17:05
    Fuji Kusaka
    0

    Hi Steve,

    the only reason am using multiple levels of inheritance is because only a small section of child templates differ from their parent. But yes do understand the fact we need to use only one RenderBody.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 26, 2015 @ 18:29
    Fuji Kusaka
    0

    What i need to know is how to get this working with Nested Templates with Multiple Levels of inheritance. Like for eg

    Home 
    Page 1 (Sub Template 1)
    Page 2 (Sub Template 2)
    Page 3 (Template 1) 
    

    The only difference between Template 1 and the Sub is they contain a list of articles. All Sub Template 1 & 2 add some more dynamic content articles in it at the footer.

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 26, 2015 @ 19:12
    Steve Morgan
    0

    I'd forget the idea of multiple inheritance here. By the sound of it you'd be better off creating pickable "components" and using a razor macro to determine what is output.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 27, 2015 @ 10:47
    Fuji Kusaka
    0

    ok but i should be able to have multiple level of inheritance and apply a specific template to any other node.

    Tried this but no success

    @section InnerBtn{
    @RenderSection("subBtn", false)
    }
    

    Any idea ?

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 28, 2015 @ 11:49
    Steve Morgan
    0

    Hi Fuji,

    Remember that your content structure has nothing/little to do with the templates structure.

    Sections are used to render bits of the code outside of the main child body (for example to add "custom" JS or footer changes) - not sure why you'd call another RenderSection from within a section? That said it looks like you can do it: http://stackoverflow.com/questions/8578843/rendersection-in-nested-razor-templates

    It's hard to help you without fully understanding what you're trying to achieve but I'm pretty sure you're heading off in the wrong direction?! Perhaps give us some more info on the problem your trying to solve and the Our Community can point you in the right direction!?

    Sorry - I know it's annoying when you feel one step away from the solution and someone is taking you back to square one!

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 28, 2015 @ 12:16
    Fuji Kusaka
    0

    Hi Steve,

    Here is how my content structure looks like right now.

    - Home
    -- Folder 1
    --- Text Page 1
    ---- Sub Text Page 1.1
    ---- Sub Text Page 1.2
    --- Text Page 2
    ---- Sub Text Page 2.1
    ---- Sub Text Page 2.2
    --- Text Page 3
    

    Template Structure

    - Master
    -- Html
    --- Temp 1
    ---- Sub Temp 1
    ---- Sub Temp 2
    ---- Sub Temp 3
    

    Text Page 1 - Text Page 3 all have the same DocType but somehow have different Child node with different DocType.

    So Text Page 1 (template Sub Temp 1 to display child nodes), and Text Page 2 (Template Sub Temp 2 to related child nodes)

    But since Text Page 3 has no child thus this will only use default template which is the Temp 1 Template but somehow the page is giving an output error when adding another RenderSection("anySection", false) within a Section.

    Templates
    -- Html > 
    <div class="m">
    @RenderBody()
    @RenderSection("intro", required:false) 
    </div>
    
    --- Temp 1
    Section intro{
      <!-- here i need some other content for SUB TEXT PAGE 1 -->
    
    
    }
    

    Hope it is making more sense what am trying o do.

    //fuji

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 28, 2015 @ 12:38
    Steve Morgan
    0

    I think we're getting there... . so is Sub Text Page 1.1 just a container for text and content that's only to be rendered in Text Page 1 or a page in it's own right?

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 28, 2015 @ 12:51
    Fuji Kusaka
    0

    Page content itself

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 28, 2015 @ 13:06
    Steve Morgan
    0

    Sorry, to be sure I follow you - it's a page or it's just used for content in the parent node?

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 28, 2015 @ 15:47
    Fuji Kusaka
    0

    hi steve it is a page on its own.

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 28, 2015 @ 16:29
    Steve Morgan
    0

    So the only difference with Text Page 3 is that it needs to output different content if it has no child nodes - you could just have this as a bit of razor logic in the template or as a Partial Macro View - I think you're trying to use sections for the wrong use!

    Templates should be reused - only use different templates for different "designs" of pages - not for content differences.

    I'm still unsure why you're trying to nest templates to match the content structure - the two are totally independent. What I think you want to do is to is to create the following template structure:

    - Master
    -- Text Page
    -- Sub Text Page
    

    And use the content on the pages?!

    I think you're a bit lost on how Umbraco works - apologies if you're not and it's just a misunderstanding.

    I'd strongly recommend you taking half an hour and running through https://our.umbraco.org/documentation/tutorials/creating-basic-site/ - creating a new project and stepping through it. It should explain all of the doc type, templates and content concepts you need.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 28, 2015 @ 17:30
    Fuji Kusaka
    0

    Steve am just mixing up v4 & v7. Not doing the right thing and quite a bit rusty i must say.

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Oct 28, 2015 @ 20:30
    Steve Morgan
    0

    Totally understandable! I've just been working on a legacy v4 site and it has been surprisingly confusing!

    Hopefully it will all gel! Good luck!

Please Sign in or register to post replies

Write your reply to:

Draft