【问题标题】:Kentico - Web part zone visibility based on Child folderKentico - 基于子文件夹的 Web 部件区域可见性
【发布时间】:2018-08-22 15:18:23
【问题描述】:

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

我想做但不知道如何根据 /Carousel/ 文件夹的存在来设置 webpart 区域的可见性:如果 /Carousel/ 文件夹在那里 -> 显示 webpart 区域/中继器,如果不是 -> 隐藏它。谢谢!

【问题讨论】:

    标签: kentico


    【解决方案1】:

    由于您想隐藏转发器,只需将转发器的“无数据行为”属性设置为在未找到任何内容时隐藏即可。

    【讨论】:

    • 感谢您的回答。我在提到“Repeater”而不是“Web part zone”时犯了错误。默认情况下,Repeater 选中了“如果未找到记录则隐藏”复选框。隐藏转发器的原因是不够的,因为转发器外面的Webpart区域是灰色的背景,没有转发器,灰色框仍然存在。如果 Carousel 文件夹不存在,我想隐藏灰色框(webpart 区域)。很抱歉造成混乱。
    【解决方案2】:

    我多次遇到同样的事情,所以我实际上专门为这种类型的东西构建了一个宏(因为正如你评论的那样,可见性是在 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;
            }
    

    【讨论】:

    • 我想我可能会得到这样的结果:让 Carousel 文件夹成为页面的最后一个子文件夹,然后将此宏添加到可见性:{%CurrentDocument.Children.LastItem.GetValue("DocumentName"). Contains("Carousel")#%} 我不明白的是,如果我将 Carousel 向上移动 -> 它不再是最后一个孩子 -> 我希望内容停止显示;但是,它并没有按预期工作。
    • 这是一种非常老套的做法,如果订单发生变化,很容易导致问题,我不推荐这样做,这不是好的做法,而且我已经看到内容因这样的事情而爆炸(就像一个 Web 部件中的开始标签和另一个 Web 部件中的结束标签一样,另一个被移动,现在您没有结束标签并且网站混乱了)。使用自定义宏,它会做你想做的。
    • 如果你真的想走那条路线,它不起作用的原因是你需要设置一个顺序,以便它按节点顺序排序。否则,它只是任何孩子。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 2020-09-20
      • 2018-10-07
      • 2021-05-07
      • 2018-01-09
      • 2016-10-18
      • 2010-12-26
      • 1970-01-01
      相关资源
      最近更新 更多