RenderTemplate with specific folders / ViewLocationFormats
Hi there,
my name is Jan I'm new in using Umbraco. :)
I try to use subfolders for my templates (views) and this is why I'm registering these folders in "ViewLocationFormats" at the application start.
It works fine for automatically rendered templates (just calling the URL) but does not when I'm calling Umbraco.RenderTemplate (nodeID) inside a template by razor code.
Has anyone an idea that could help me? Maybe I'm doing something wrong or there is an alternative to "RenderTemplate".
If you want to render a 'different template view' for the content other than the one it is published under, you can use ?altTemplate=othertemplatealias in the querystring
eg
/hompage/news?altTemplate=altNewsListing
would load the news page node with the altNewsListing template
furthermore so would:
/homepage/news/altNewsListing
this is default behaviour for versions of Umbraco, but can be turned off in umbracoSettings.config for versions 7.2.*
The way I normally kind of do child component templating thing is with partial views: eg
foreach(var child in page.Children) {
var partialViewName = "ListItem" + child.DocumentTypeAlias;
@Html.Partial(partialViewName, child)
}
Then I have a matching partial view name called
ListItemDocTypeAlias
which inherits:
@inherits UmbracoViewPage<IPublishedContent>
and where DocTypeAlias is the alias of the doctype you are looping through.
Because you pass the child content as the 'model' inside the Partial View you can read all the properties of the child node, and write out the relevant html for your loop...
In the last weeks I did not work on Umbraco - so let me first thank you for your fast answer!
I'm not sure what's the benefit in using partial views instead of "regular" templates because you have to use this specific naming convention and on the other hand it could work with templates "naturally".
What's your reason for doing it this way? Maybe it's better to keep the number of templates a small as possible?
I think I slightly misunderstand your question originally, I would use the approach described above, if the children, or items to loop through were 'picked' and the items all had different doctypes / and required different html templates for displaying each item in the loop.
If you just want to loop through the child nodes of a page, then yes it just makes sense to do that in main page template view and have:
foreach(var child in page.Children) {
<li><a href="@child.Url">@child.Name</a></li>
}
etc
to write out your child content items in your desired html for each item.
I'd put this into a partial (or macro partial) if I wanted to reuse this looping / html on another view-template.
I have only used Umbraco.RenderTemplate with XSLT in the past, so I'm not sure, but I think it also takes an optional second parameter of the templateId, you would like to render the node eg:
Umbraco.RenderTemplate(nodeId,templateId)
Maybe that would help it find the appropriate template from your views subfolders. Be interested to know if that 'works'
RenderTemplate with specific folders / ViewLocationFormats
Hi there,
my name is Jan I'm new in using Umbraco. :)
I try to use subfolders for my templates (views) and this is why I'm registering these folders in "ViewLocationFormats" at the application start.
It works fine for automatically rendered templates (just calling the URL) but does not when I'm calling Umbraco.RenderTemplate (nodeID) inside a template by razor code.
Has anyone an idea that could help me? Maybe I'm doing something wrong or there is an alternative to "RenderTemplate".
Thanks in advance!
Jan
Hi Jan
Are you using MVC Views ?
Html.Partial("PartialViewName")
or
Html.Partial("~/pathto/partialview.cshtml")
should render a partial view in a template.
If you want to render a 'different template view' for the content other than the one it is published under, you can use ?altTemplate=othertemplatealias in the querystring
eg
/hompage/news?altTemplate=altNewsListing
would load the news page node with the altNewsListing template
furthermore so would:
/homepage/news/altNewsListing
this is default behaviour for versions of Umbraco, but can be turned off in umbracoSettings.config for versions 7.2.*
by disabling alternative templates
eg:
Hi Marc,
thank you very much for your answer.
The view is generated by Umbraco and I just moved it to my subfolder. In the view I iterate through the child nodes.
foreach (var child in page.Children) { @Umbraco.RenderTemplate(child.Id); @child.Id }
Could this work with a partial view? I think there wouldn't be a connection to the Umbraco content and its fields or am I wrong?
To render a different template maybe will be interesting later but in this case I want to render the "content template".
Thanks
Jan
Hi Jan
The way I normally kind of do child component templating thing is with partial views: eg
Then I have a matching partial view name called
ListItemDocTypeAlias
which inherits:
and where DocTypeAlias is the alias of the doctype you are looping through.
Because you pass the child content as the 'model' inside the Partial View you can read all the properties of the child node, and write out the relevant html for your loop...
if that makes sense ?
Sorry for this late answer, Marc!
In the last weeks I did not work on Umbraco - so let me first thank you for your fast answer!
I'm not sure what's the benefit in using partial views instead of "regular" templates because you have to use this specific naming convention and on the other hand it could work with templates "naturally".
What's your reason for doing it this way? Maybe it's better to keep the number of templates a small as possible?
Thanks for your friendly help! :)
Jan
Hi Jan
I think I slightly misunderstand your question originally, I would use the approach described above, if the children, or items to loop through were 'picked' and the items all had different doctypes / and required different html templates for displaying each item in the loop.
If you just want to loop through the child nodes of a page, then yes it just makes sense to do that in main page template view and have:
etc
to write out your child content items in your desired html for each item.
I'd put this into a partial (or macro partial) if I wanted to reuse this looping / html on another view-template.
I have only used Umbraco.RenderTemplate with XSLT in the past, so I'm not sure, but I think it also takes an optional second parameter of the templateId, you would like to render the node eg:
Maybe that would help it find the appropriate template from your views subfolders. Be interested to know if that 'works'
Hi Marc,
thanks again for your support.
In general it works to use the templateID but in my special case - views in a sub folder - it still does not.
I think I have to take some more time or ask an "Umbraco agency".
Thank you so far for your fast und very friendly help! :)
Jan
is working on a reply...