【发布时间】:2018-08-22 15:18:23
【问题描述】:
我在 webpart 区域中有一个带有转发器的页面,用于显示旋转内容(在轮播中)。轮播的内容来自 /Carousel/ 文件夹,它是页面本身的子文件夹。
我想做但不知道如何根据 /Carousel/ 文件夹的存在来设置 webpart 区域的可见性:如果 /Carousel/ 文件夹在那里 -> 显示 webpart 区域/中继器,如果不是 -> 隐藏它。谢谢!
【问题讨论】:
标签: kentico
我在 webpart 区域中有一个带有转发器的页面,用于显示旋转内容(在轮播中)。轮播的内容来自 /Carousel/ 文件夹,它是页面本身的子文件夹。
我想做但不知道如何根据 /Carousel/ 文件夹的存在来设置 webpart 区域的可见性:如果 /Carousel/ 文件夹在那里 -> 显示 webpart 区域/中继器,如果不是 -> 隐藏它。谢谢!
【问题讨论】:
标签: kentico
我多次遇到同样的事情,所以我实际上专门为这种类型的东西构建了一个宏(因为正如你评论的那样,可见性是在 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\"") %}
【讨论】: