I feel like I'm flying blind here with absolutely no documentation on version 6...
I'm assuming DynamicNode is no longer available in v6. If that's true, can someone at least update/add a new cheatsheat? http://our.umbraco.org/documentation/Cheatsheets/DynamicNodeRazor.pdf. 95% of the code samples I've seen all use DynamicNode, which don't seem to work as partial view macros (.cshtml) on version 6+
I tried writing my own recursive navigation script, but realize I can't even write a recursive funciton since I don't even know what "nodes" are typecasted as without diving into the full source version. Does anyone have a sample as a starting point?
Also it seems like in order to get anything done with a partial view macro I need to inherit Umbraco.Web.Mvc.UmbracoTemplatePage not the default Umbraco.Web.Macros.PartialViewMacroPage. Am I doing something wrong?
Thanks for the links, but have read most of that (which also leads me to the outdated razor cheatsheet). Although the MVC section is somewhat helpful, it's exceedingly weak and I struggle to make significant progress even with prior MVC/Razor experience.
For instance, this line for example straight out of the docs:
@foreach(var child in Model.Content.Children())
What type of variable is child? What are its properties and methods? Do I need to dive into the Umbraco source code to figure this out?
Children returns an IEmumerable<IPublishedContent> - but I can't see a class reference for this in the docs yet either. I'd suggest the easiest way at this point would be to code your razor in VS.Net - as this is strongly typed, you'll get intellisense, which will reveal the properties and methods.
One of my frequent tricks is to just temporarily dump the type of the variable on the page so I can see it right there rather than ever trying to deduce from documentation or source code. For example, your question about what type of variable is "child"?
<p>Type is @Model.Content.Children().GetType().ToString()</p>
Output:
Type is IEnumerable<IPublishedContent> (or whatever)
So then if you are enumerating this you know what the individual child variable is. Or, if you code in Visual Studio, just hover over the var keyword and it will tell you there too. Eventually however you'll start to learn these and hopefully gets easier each time.
Thanks to everyone for their assistance, especially in identifying IPublishedContent (it is my understanding this has replaced DynamicNode in v4.10+). Here's my first stab at a v6 recursive razor menu. Some variables should probably be passed via the template, but I'm open to critiques.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ var home = Model.Content.AncestorOrSelf(1); }
@*Loop through Home children*@ @foreach (var node in home.Children.Where(x => x.IsVisible())) { @RecursiveMenu(node) } </ul>
@helper RecursiveMenu(IPublishedContent node) { var active = ""; var levelLimit = 3; // adjust as necessary
if(Model.Content.AncestorOrSelf(1).Id != CurrentPage.Id) { @*See if we should show current page and ancestors as active*@ if (node.Id == Model.Content.AncestorOrSelf(2).Id || node.Id == CurrentPage.Id) { active = "active"; } }
@*Returns true if current doctype is found in allowed list*@ if(IsViewableDoctype(node.DocumentTypeAlias)) { <li class="@active"> <a href="@node.Url">@node.Name</a> @if(node.Children.Count() > 0 && node.Level < levelLimit) { <ul> @foreach (var subNode in node.Children.Where(x => x.IsVisible())) { @RecursiveMenu(subNode) } </ul> } </li> } }
@functions{ public bool IsViewableDoctype(string doctype) { // Filters to show only nodes of listed doctype aliases - adjust as necessary string[] validDoctypes = {"homepage", "landingPage", "contentPage"};
@*See if we should show current page and ancestors as active*@ if (node.Id == Model.Content.AncestorOrSelf(2).Id || node.Id == CurrentPage.Id) { active = "active"; }
If I'm several levels deep, it will only show the current page and first level as active (not levels in between). Can someone point me in the right direction?
v6 razor docs?
I feel like I'm flying blind here with absolutely no documentation on version 6...
I'm assuming DynamicNode is no longer available in v6. If that's true, can someone at least update/add a new cheatsheat? http://our.umbraco.org/documentation/Cheatsheets/DynamicNodeRazor.pdf. 95% of the code samples I've seen all use DynamicNode, which don't seem to work as partial view macros (.cshtml) on version 6+
I tried writing my own recursive navigation script, but realize I can't even write a recursive funciton since I don't even know what "nodes" are typecasted as without diving into the full source version. Does anyone have a sample as a starting point?
Also it seems like in order to get anything done with a partial view macro I need to inherit Umbraco.Web.Mvc.UmbracoTemplatePage not the default Umbraco.Web.Macros.PartialViewMacroPage. Am I doing something wrong?
Here's what documentation there is currently. In particular worth reading through the MVC section.
Andy
Thanks for the links, but have read most of that (which also leads me to the outdated razor cheatsheet). Although the MVC section is somewhat helpful, it's exceedingly weak and I struggle to make significant progress even with prior MVC/Razor experience.
For instance, this line for example straight out of the docs:
What type of variable is child? What are its properties and methods? Do I need to dive into the Umbraco source code to figure this out?
Not to mention my original questions.
Children returns an IEmumerable<IPublishedContent> - but I can't see a class reference for this in the docs yet either. I'd suggest the easiest way at this point would be to code your razor in VS.Net - as this is strongly typed, you'll get intellisense, which will reveal the properties and methods.
One of my frequent tricks is to just temporarily dump the type of the variable on the page so I can see it right there rather than ever trying to deduce from documentation or source code. For example, your question about what type of variable is "child"?
Output:
Type is IEnumerable<IPublishedContent>
(or whatever)So then if you are enumerating this you know what the individual child variable is. Or, if you code in Visual Studio, just hover over the
var
keyword and it will tell you there too. Eventually however you'll start to learn these and hopefully gets easier each time.Best of luck!
Good articles :
http://www.ben-morris.com/using-umbraco-6-to-create-an-asp-net-mvc-4-web-applicatio
http://24days.in/umbraco/2012/the-tale/
Comment author was deleted
There is also the razor chapters on the new tv site (aimed at v6)
http://beta.umbraco.tv/videos/implementor/working-with-umbraco-data/razor-syntax/introduction-to-razor/
http://beta.umbraco.tv/videos/implementor/working-with-umbraco-data/querying-umbraco-data-with-razor/currentpage-object/
Thanks to everyone for their assistance, especially in identifying IPublishedContent (it is my understanding this has replaced DynamicNode in v4.10+). Here's my first stab at a v6 recursive razor menu. Some variables should probably be passed via the template, but I'm open to critiques.
There looks to be a small bug with this line:
If I'm several levels deep, it will only show the current page and first level as active (not levels in between). Can someone point me in the right direction?
is working on a reply...