I'm putting together some sites using Razor Layouts and I'm finding the templating engine a little odd/restrictive. So I can only assume, I am doing something wrong.
_Layout.cshtmlContains the core layout (head, footer, etc)
Article.cshtmlContains the parts needs to display an Article
Home.cshtmlContains the homepage layout.
Now where I come unstuck is, then injecting content into Article. Surely I place an RenderBody on article, and this should allow me to inject a subsequent template into Article.
Following this principle, I could then inject content into LandingPage if it had a RenderBody(), which is super nice! However, as soon as the template has a RenderBody(), it won't serve in Umbraco.
E.g.
I want to be able to use Article.cshtml and LandingPage.cshtml as templates in Umbraco, and also be able to inject content into those templates, as you can do using a ContentPlaceHolder with WebForms.
@{ Layout = "Site.cshtml"; @} @section siteHead { <!-- Page header stuff (if any) then define new section that nested templates can use --> @RenderSection("pageHead", required:false) } <div id="pageContent"> @RenderBody() </div>
Views/Page1.cshtml
@{ Layout = "Page.cshtml"; } @section pageHead { <!-- stuff in header for this page --> } <div>page content</div>
Page1.cshtml should then render as:
<head> <!-- Page header stuff (if any) then define new section that nested templates can use --> <!-- stuff in header for this page --> </head> <div id="siteContent"> <div id="pageContent"> <div>page content</div> </div> </div>
I agree, I think when it comes to default content within a section, masterpages does look tidier. However as you say, you can achieve the same thing with sections and IsSectionDefined….
I also had beef with this when converting an Umbraco masterpages site to Mvc my conclusion was to just have an extra template with nothing in it to get around the 'cannot be requested directly' bullshit
Not exactly great because you end up with more benign templates so I'm all ears for a better solution. I just tried the following using your suggestion above and it works:
//Only call RenderBody if not on the main Document type @if(Model.Content.DocumentTypeAlias != "BigDaddy"){
@RenderBody() // and any ifSectionDefined stuff
}
I'm keen to hear from others more experienced than I whether the above is just asking for trouble :)
This really is quite ugly. I've been messing around all afternoon with this, but RenderBody() / RenderSection() just cannot exist on a View. You can do some nasty nested nonense, but that becomes worse than masterpages.
Razor Layouts - RenderBody() = Unusable Template
Hello,
I'm putting together some sites using Razor Layouts and I'm finding the templating engine a little odd/restrictive. So I can only assume, I am doing something wrong.
_Layout.cshtml Contains the core layout (head, footer, etc)
Article.cshtml Contains the parts needs to display an Article
Home.cshtml Contains the homepage layout.
Now where I come unstuck is, then injecting content into Article. Surely I place an RenderBody on article, and this should allow me to inject a subsequent template into Article.
_Layout.cshtml
-> Article.cshtml
-> LandingPage.cshtml
Following this principle, I could then inject content into LandingPage if it had a RenderBody(), which is super nice! However, as soon as the template has a RenderBody(), it won't serve in Umbraco.
E.g.
I want to be able to use Article.cshtml and LandingPage.cshtml as templates in Umbraco, and also be able to inject content into those templates, as you can do using a ContentPlaceHolder with WebForms.
Doh!
_Layout.cshtml
_Article.cshtml
LandingPage.cshtml
This is essentially what I am trying to do:
http://aboutcode.net/2010/11/08/nested-layout-pages-in-razor.html
But it results in
The file "~/Views/Layout.cshtml" cannot be requested directly because it calls the "RenderBody" method.
Did you read this great Introducing “Razor” blog? Might help :-).
Jeroen
I have :) and it is very helpful, but just not with the nested bit ;)
I can see how it could be made using 'section, false', but that seems a bit of a round about way of doing things.
Is it that Razor Layouts do not support nested templates where each template can be used?
It seems like for each layer of nesting, I would have to create a 'servable' template, resulting in twice as many templates.
I seem to remember you can only fill sections defined in the template, not 'grandparent' ones.
So for example (with huge chunks stripped out, obviously!):
Views/Site.cshtml
Views/Page.cshtml
Views/Page1.cshtml
Page1.cshtml should then render as:
Does that make sense / help at all?
Lotte
I'm not 100% sure it's what your after but have you found the IsSectionDefined method? More info here
Using sections and IsSectionDefined does achieve what I'm after, but it does rather seem like over kill.
It seems you either, need to have an extra template for each 'inheritable' template to make it usable.
Or two copies of the template, one thats inheritable and that isnt.
Suddenly masterpages look like a really tidy solution!
I agree, I think when it comes to default content within a section, masterpages does look tidier. However as you say, you can achieve the same thing with sections and IsSectionDefined….
Would it be possible to wrap RenderBody() in something, so the inhertiable template could be served?
Using RenderBody() makes the child templates so clean, and well Razorfied ;)
I also had beef with this when converting an Umbraco masterpages site to Mvc
my conclusion was to just have an extra template with nothing in it to get around the 'cannot be requested directly' bullshit
Not exactly great because you end up with more benign templates so I'm all ears for a better solution.
I just tried the following using your suggestion above and it works:
I'm keen to hear from others more experienced than I whether the above is just asking for trouble :)
- Tim
Interesting,
I tend name my templates 1 to 1 against my document types.
E.g. if Base is my root document type that every inherits off, it makes sense that my root template is called Base and is servable.
Has anybody got any thoughts on the above if statement, it looks like a nice way of doing it myself.
Otherwise, it's a case of ditching render body and just using sections right? / have lots of extra empty templates.
This really is quite ugly. I've been messing around all afternoon with this, but RenderBody() / RenderSection() just cannot exist on a View.
You can do some nasty nested nonense, but that becomes worse than masterpages.
For anyone else encountering this issue, the solution proposed by @wolulcmit worked great for me. Thanks!
In a nutshell:
I my case same error
but the issue was Content > Template.
let say I have created a content but it's Template was not selected to Child Template which used Render body :)
So I selected / change the template and it start working
is working on a reply...