Umbraco AncestorOrSelf(int) - 它有什么作用? [英] Umbraco AncestorOrSelf(int) - what does it do?

查看:23
本文介绍了Umbraco AncestorOrSelf(int) - 它有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用时:

@Model.AncestorOrSelf(3)

在 Umbraco 的 .cshtml 模板中,这可能会将节点遍历限制为 3 个级别.这是否正确,如果是,是否还有人可以确认当前节点的索引是否为零?

解决方案

@Model.AncestorOrSelf(3)

<块引用>

Model.Content 是我们所在的当前页面.AncestorsOrSelf 就是全部此页面在树中的祖先.(level) 表示:上升到级别 1/2/3/... 并且当您获得时停止寻找更多祖先

以上是您对 Umbraco 7.x rc 版本的评论.

以下面的内容树为例,它类似于您通常在 umbraco 管理区域的内容部分中看到的内容:

每个内容文档都有一个级别,默认情况下从 1 开始.

<块引用>

在 Umbraco 的 .cshtml 模板中,这大概会限制节点遍历到 3 个级别

正如您在下面的示例中所看到的,级别不断增加 - 级别 + 1.因此,它从 1 开始,然后继续向您的子级别添加 1.

- 内容-- 家(级别 = 1)-- 关于我们 (level = 2)-- 联系我们(级别 = 2)-- 新闻区(级别= 2)-- 新闻项目 1(级别 = 3)-- 新闻项目 2(级别 = 3)-- 其他节点(级别 = 1)

因此,当您提到 3 作为 AncestorOrSelf 的参数时,您要求从当前元素移动到树中的第 3 级,该元素可以是任何文档/部分视图,并在找到时停止寻找更多祖先.

AncestorOrSelf(level) 返回一个单项,如果它是 DynamicPublishContent 类型,即您将可以访问许多属性,例如 id、name、url 等.

@CurrentPage.AncestorOrSelf(1)//基于上面的内容结构,上面的语句会给你一个项目 - Home.

它基本上是按级别获取祖先,与您当前的级别或当前页面对象无关.

例如,如果您想在您的主布局中创建一个导航以便在您网站的所有页面上共享它,您将在您的模板中执行以下操作:

    @foreach(var page in @CurrentPage.AncestorOrSelf(1).Children){<li><a href="@page.Url">@page.Name</a></li>}

根据我们的示例,它将为您提供:

关于我们、联系我们、新闻区(以列表形式和适当的链接)

When using:

@Model.AncestorOrSelf(3)

In a .cshtml template in Umbraco, this would presumably limit the node traversal to 3 levels. Is this correct, and if so can anyone also confirm if the current node has the index zero?

解决方案

@Model.AncestorOrSelf(3)

Model.Content is the current page that we're on. AncestorsOrSelf is all of the ancestors this page has in the tree. (level) means: go up to level 1/2/3/... and stop looking for more ancestors when you get there.

Above is the comment that you get with Umbraco 7.x rc version.

Take an example of the content tree below that is kind of similar to that you normally see in contents section in umbraco admin area:

Each content document has a level and by default it starts with 1.

In a .cshtml template in Umbraco, this would presumably limit the node traversal to 3 levels

As you can see in the example below, the level gets on increasing - level + 1. so, it starts by 1 and then just go on adding 1 to your sub levels.

- Content
 -- Home (level = 1)
   -- About Us (level = 2)
   -- Contact Us (level = 2)
   -- News Area (level = 2)
     -- News Item 1 (level = 3)
     -- News Item 2 (level = 3)
 -- Other Node (level = 1)

So when you mention 3 as parameter for AncestorOrSelf, you are asking to move to 3rd level in the tree from the current element that can be any document/partial view and stop looking for any more ancestors when its found.

AncestorOrSelf(level) returns a single item which if of type DynamicPublishContent i.e. you will have access to many properties like id, name, url, etc.

@CurrentPage.AncestorOrSelf(1)
// based on content structure above, the above statement will give you an item - Home.

It is basically for fetching ancestors by level, doesn't matter what your current level or currentpage object is.

For example, if you want to create a navigation in your main layout so as to share it on all pages of your site, you will do something like this in your template:

<ul>
 @foreach(var page in @CurrentPage.AncestorOrSelf(1).Children)
 {
   <li><a href="@page.Url">@page.Name</a></li>
 }
</ul>

Based on our example, it will give you:

About Us, Contact Us, News Area (in list form and with proper links)

这篇关于Umbraco AncestorOrSelf(int) - 它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆