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."
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.
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.
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.
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.
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!
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 -->
}
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?
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.
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 ?
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:
You only use the RenderSection for content in areas outside the "main" child content (usually the div with id=container etc).
Steve
Hi Steve,
Sorry my formatting was not good
I actually have this template structure
I need to Apply Sub Template 1 on some node but its returning me this error on pages where Template 1 is used
This is how my Master looks like
Template 1
Sub Template 1
By the way am converting an old umbraco to mvc
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
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.
What i need to know is how to get this working with Nested Templates with Multiple Levels of inheritance. Like for eg
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.
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.
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
Any idea ?
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!
Hi Steve,
Here is how my content structure looks like right now.
Template Structure
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.
Hope it is making more sense what am trying o do.
//fuji
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?
Page content itself
Sorry, to be sure I follow you - it's a page or it's just used for content in the parent node?
hi steve it is a page on its own.
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:
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.
Steve am just mixing up v4 & v7. Not doing the right thing and quite a bit rusty i must say.
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!
is working on a reply...