Kentico - 基于子文件夹的 Web 部件区域可见性

Kentico - Web part zone visibility based on Child folder

我有一个页面,在 Web 部件区域内有一个转发器,用于显示旋转内容(在轮播中)。旋转木马的内容来自 /Carousel/ 文件夹,它是页面本身的子文件夹。

我想做但不知道如何根据 /Carousel/ 文件夹的存在设置 webpart 区域的可见性:如果 /Carousel/ 文件夹存在 -> 显示 webpart zone/repeater, 如果没有 -> 隐藏它。谢谢!

既然你想隐藏转发器,只需将转发器的 "No data behavior" 属性 设置为在没有找到时隐藏。

我已经 运行 多次进入同样的事情,所以我实际上专门为这种类型的东西构建了一个宏(因为正如你评论的那样,可见性在 webpart 区域而不是中继器)。

这里是宏方法

            [MacroMethod(typeof(bool), "Finds if at least 1 child exists under the given node for navigation purposes",1 )]
            [MacroMethodParam(0, "int", typeof(int), "The Node ID")]
            [MacroMethodParam(1, "Text", typeof(string), "A list of valid page types, comma, semi-colon, or bar seperated")]
            [MacroMethodParam(2, "bool", typeof(bool), "Include Unpuglished pages (false by default)")]
            [MacroMethodParam(3, "bool", typeof(bool), "Include Documents Hidden from Navigation (false by default)")]
            public static object NodeHasNavigationChildren(EvaluationContext context, params object[] parameters)
            {
                switch (parameters.Length)
                {
                    case 1:
                        return HBS_Gen_Module.GeneralHelpers.NodeHasNavigationChildren(ValidationHelper.GetInteger(parameters[0], -1));
                    case 2:
                        return HBS_Gen_Module.GeneralHelpers.NodeHasNavigationChildren(ValidationHelper.GetInteger(parameters[0], -1), ValidationHelper.GetString(parameters[1], ""));
                    case 3:
                        return HBS_Gen_Module.GeneralHelpers.NodeHasNavigationChildren(ValidationHelper.GetInteger(parameters[0], -1), ValidationHelper.GetString(parameters[1], ""), ValidationHelper.GetBoolean(parameters[2], false));
                    case 4:
                        return HBS_Gen_Module.GeneralHelpers.NodeHasNavigationChildren(ValidationHelper.GetInteger(parameters[0], -1), ValidationHelper.GetString(parameters[1], ""), ValidationHelper.GetBoolean(parameters[2], false), ValidationHelper.GetBoolean(parameters[3], false));
                    default:
                    case 0:
                        throw new NotSupportedException();
                }
            }

下面是它调用的实际方法:

/// <summary>
        /// Checks if there exists a Node that should be displayed in navigation, this is more advanced than the "NodeHasChildren" which only checks for published children, and doesn't have page type and DocumentHiddenInNavigation checks
        /// </summary>
        /// <param name="NodeID">The Node ID</param>
        /// <param name="PageTypes">Semi-colon, bar, or comma seperated list of class names</param>
        /// <param name="IncludeUnpublished">If unpublished should be included (default false)</param>
        /// <param name="IncludeHiddenNavigationDocuments">If Documents hidden from navigation should be included (default false)</param>
        /// <returns>If there exists at least 1 child that fits</returns>
        public static bool NodeHasNavigationChildren(int NodeID, string PageTypes = "", bool IncludeUnpublished = false, bool IncludeHiddenNavigationDocuments = false)
        {
            return CacheHelper.Cache(cs => NodeHasNavigationChildrenCache(cs, NodeID, PageTypes, IncludeUnpublished, IncludeHiddenNavigationDocuments), new CacheSettings(1440, new string[] { "NodeHasNavigationChildren", NodeID.ToString(), PageTypes, IncludeUnpublished.ToString(), IncludeHiddenNavigationDocuments.ToString() }));
        }
        private static bool NodeHasNavigationChildrenCache(CacheSettings cs, int NodeID, string PageTypes = "", bool IncludeUnpublished = true, bool IncludeHiddenNavigationDocuments = false)
        {
            var ChildQuery = DocumentHelper.GetDocuments().WhereEquals("NodeParentID", NodeID).Columns("NodeID").TopN(1);
            if (IncludeUnpublished)
            {
                ChildQuery.Published(false);
            }

            if (IncludeHiddenNavigationDocuments)
            {
                ChildQuery.Where("DocumentMenuItemHideInNavigation = 0 or DocumentMenuItemHideInNavigation = 1");
            }
            else
            {
                ChildQuery.WhereEquals("DocumentMenuItemHideInNavigation", false);
            }

            if (!string.IsNullOrWhiteSpace(PageTypes))
            {
                ChildQuery.WhereIn("ClassName", PageTypes.Split(";|,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
            }

            if(cs.Cached)
            {
                TreeNode ParentNode = DocumentHelper.GetDocuments().WhereEquals("NodeID", NodeID).Published(false).FirstObject;
                if(ParentNode != null) {
                    cs.CacheDependency = CacheHelper.GetCacheDependency("node|"+SiteContext.CurrentSiteName+"|" + ParentNode.NodeAliasPath + "|childnodes");
                }
            }

            return ChildQuery.Count > 0;
        }

您不必实施任何自定义宏方法。在这种情况下,您可以简单地利用 Kentico 的默认方法,例如 Exists

{% CurrentDocument.Children.Exists("DocumentName==\"Carousel\"") %}