Using 4.7 I know it's possible to have something like Model.AncestorOrSelf(level). But what would be the best way to get the closest ancestor(or self) of a perticular documenttype?
Hm, not getting the result I'm hoping for. I tried to replace a bit of code out of a razor-snippet. I was hoping the two would have the same result, but it appears as the second one renders zero children
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level); var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass); //var parent = @Model.AncestorOrSelf(level); var parent = @Model.AncestorOrSelf.Where("NodeTypeAlias == @0", "Start"); if (parent != null) { <ul@Html.Raw(ulClass)> @foreach (var item in parent.Children.Where("Visible")) { var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : ""; <li@Html.Raw(selected)> <a href="@item.Url">@item.Name</a> </li> } </ul> }
Running visual studio on the umbraco source alongside any kind of umbraco development and using the Object Browser as you did is really the only way to be effective in my opinion. So many nuances which are just easier to look at the source than digging or searching around the net. The cheat sheet also is a great reference for getting started (I have mine printed out and always within reach myself) but not totally comprehensive, not quite like the Object Browser is at least. Best of luck to you!
AncestorOrSelf by document type
Hello,
Using 4.7 I know it's possible to have something like Model.AncestorOrSelf(level). But what would be the best way to get the closest ancestor(or self) of a perticular documenttype?
/D
That would be by including it in a Where statement:
@Model.AncestorOrSelf.Where("NodeTypeAlias == @0", "yourDocumentTypeAlias")
Hm, not getting the result I'm hoping for. I tried to replace a bit of code out of a razor-snippet. I was hoping the two would have the same result, but it appears as the second one renders zero children
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass);
//var parent = @Model.AncestorOrSelf(level);
var parent = @Model.AncestorOrSelf.Where("NodeTypeAlias == @0", "Start");
if (parent != null) {
<ul@Html.Raw(ulClass)>
@foreach (var item in parent.Children.Where("Visible")) {
var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<li@Html.Raw(selected)>
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
}
@Model.AncestorsOrSelf("name-of-doctype").Last()
Hm, still the same result.
Using var parent = @Model.AncestorOrSelf(level); results in @parent.Children.Count() == 11
while
var parent = @Model.AncestorOrSelf("Start").Last()results in @parent.Children.Count() == 0;
Notice the extra "s" in ancestorsOrSelf... it's a collection.. ;)
Brilliant. Thanks!
Where are these APIs documented?
I have paid for umbraco.tv, but i cannot find anything on the site that lets me work these things out for myself.
Hi Baldy.
Did you looking for documentation for Razor in Umbraco. If so I think these links will be good:
http://umbraco.tv/videos/implementor/working-with-umbraco-data/razor-syntax/introduction-to-razor/
http://umbraco.tv/videos/implementor/working-with-umbraco-data/querying-umbraco-data-with-razor/
http://our.umbraco.org/documentation/Reference/Templating/Macros/Razor/basic-razor-syntax
http://our.umbraco.org/documentation/reference/templating/macros/razor/using-razor-axes
http://our.umbraco.org/documentation/reference/templating/Masterpages/
For working with MVC look here:
http://our.umbraco.org/documentation/reference/templating/Mvc/
I hope this is what you´re looking for.
/Dennis
Dennis, many thanks.
The last one is particularly useful as a point of reference for MVC.
I resorted to using the Object Browser to figure out what @CurrentPage had to offer (an instance of DynamicPublishedContent).
I also came across these cheat sheets, which were a huge help...
http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets
Running visual studio on the umbraco source alongside any kind of umbraco development and using the Object Browser as you did is really the only way to be effective in my opinion. So many nuances which are just easier to look at the source than digging or searching around the net. The cheat sheet also is a great reference for getting started (I have mine printed out and always within reach myself) but not totally comprehensive, not quite like the Object Browser is at least. Best of luck to you!
Hi David,
The other closest way of getting something like Model.AncestorOrSelf(level) would be
then something like
is working on a reply...