Confluence 宏:Velocity 在使用 Array.add() 时打印出不需要的 "true"
Confluence Macro: Velocity prints out unwanted "true" when using Array.add()
现在我正在为 confluence 开发一个用户宏,它创建一个空间列表,这些空间在提供的类别中。我是 Confluence 开发的新手,之前从未使用过 Velocity,但我设法让宏正常工作。
但有一个问题:每当我使用 .add()
方法时,模板都会打印出 true
,所以我从宏中得到的是一些 true
字符串 +我想要的名单。我怎样才能避免打印出 true
?为什么要打印它?
这是整个宏代码
## User Macro: spaces-in-categories
##
## Created by: Gabriel Juelke <pyriand3r@gmail.com>
## Adapted from Remo Siegwart's answer at http://web.archive.org/web/20140813094412/https://answers.atlassian.com/questions/274837/show-a-space-list-within-a-page-using-category-label
##
## @param Label:title=Space Category|type=string|required=true|desc=The categories the spaces should have. Use Pipe to separate categories.
## create list out of provided category string
#set($labelList = $paramLabel.split("\|"))
#set($spacesByLabel = [])
## try to fetch all spaces for each category
#foreach ($spaceLabel in $labelList)
#set($label = $action.labelManager.getLabel( "team:${spaceLabel}"))
#if ($!label)
#$spacesByLabel.add($action.labelManager.getSpacesWithLabel( $label ))
#else
<div class="aui-message warning">
<p class="title">
<span class="aui-icon icon-warning">Warning</span>
<strong>Space category not found</strong>
</p>
<p>Couldn't find space category <strong>$spaceLabel</strong>!</p>
</div>
#end
#end
## filter out all spaces that are not in all categories
#set($spacesInAll = [])
#set($firstList = $spacesByLabel.get(0))
#foreach ($space in $firstList)
#set($hasAll = 1)
#foreach ($list in $spacesByLabel)
#set($inList = 0)
#foreach ($item in $list)
#if ($space.name == $item.name)
#set($inList = 1)
#end
#end
#if ($inList == 0)
#set($hasAll = 0)
#end
#end
#if ($hasAll == 1)
#$spacesInAll.add($space);
#end
<ul class="spaces-by-category-user-macro">
#foreach ( $space in $spacesInAll )
## check if current user can view space
#if ( $permissionHelper.canView( $action.getAuthenticatedUser(), $space ) )
<li><a href="$!space.urlPath">$!space.name</a></li>
#end
#end
</ul>
#set ( $d = "$") ## escape the dollar sign for jQuery in velocity
<script>
## Sort the list client side as this can't be done server side in a user macro.
AJS.toInit(function (${d}) {
${d}('.spaces-by-category-user-macro').each(function(){
var ${d}me = ${d}(this);
// Sort the list items
var listItems = ${d}me.children('li').get();
listItems.sort(function(x,y) {
var compareX = ${d}(x).text().toUpperCase();
var compareY = ${d}(y).text().toUpperCase();
return (compareX < compareY) ? -1 : (compareX > compareY) ? 1 : 0;
});
// Write the list items back to the DOM
${d}.each(listItems, function(index, item) { ${d}me.append(item); });
});
});
</script>
#end
好的,我找到了一个 "workaround" 我认为...或解决方案...但对我来说,它似乎不仅仅是一种解决方法:
如果将 .add()
方法封装在 #set()
方法中,您可以将方法的输出(true
)重定向到一个变量中,以防止它被打印出来。
所以调用应该是这样的:
#set($result = $list.add($whatsoever))
我也 运行 遇到了这个问题,并试图提出一个解决方案来避免将 #set()
与虚拟变量一起使用。似乎在 Confluence 中 .add()
的输出必须封装在更多 Velocity 代码中才能不呈现。
这是我能想到的最简单的解决方案,但它仍然让我想挖出我的眼睛:
#if( $array.add($data) ) #end
现在我正在为 confluence 开发一个用户宏,它创建一个空间列表,这些空间在提供的类别中。我是 Confluence 开发的新手,之前从未使用过 Velocity,但我设法让宏正常工作。
但有一个问题:每当我使用 .add()
方法时,模板都会打印出 true
,所以我从宏中得到的是一些 true
字符串 +我想要的名单。我怎样才能避免打印出 true
?为什么要打印它?
这是整个宏代码
## User Macro: spaces-in-categories
##
## Created by: Gabriel Juelke <pyriand3r@gmail.com>
## Adapted from Remo Siegwart's answer at http://web.archive.org/web/20140813094412/https://answers.atlassian.com/questions/274837/show-a-space-list-within-a-page-using-category-label
##
## @param Label:title=Space Category|type=string|required=true|desc=The categories the spaces should have. Use Pipe to separate categories.
## create list out of provided category string
#set($labelList = $paramLabel.split("\|"))
#set($spacesByLabel = [])
## try to fetch all spaces for each category
#foreach ($spaceLabel in $labelList)
#set($label = $action.labelManager.getLabel( "team:${spaceLabel}"))
#if ($!label)
#$spacesByLabel.add($action.labelManager.getSpacesWithLabel( $label ))
#else
<div class="aui-message warning">
<p class="title">
<span class="aui-icon icon-warning">Warning</span>
<strong>Space category not found</strong>
</p>
<p>Couldn't find space category <strong>$spaceLabel</strong>!</p>
</div>
#end
#end
## filter out all spaces that are not in all categories
#set($spacesInAll = [])
#set($firstList = $spacesByLabel.get(0))
#foreach ($space in $firstList)
#set($hasAll = 1)
#foreach ($list in $spacesByLabel)
#set($inList = 0)
#foreach ($item in $list)
#if ($space.name == $item.name)
#set($inList = 1)
#end
#end
#if ($inList == 0)
#set($hasAll = 0)
#end
#end
#if ($hasAll == 1)
#$spacesInAll.add($space);
#end
<ul class="spaces-by-category-user-macro">
#foreach ( $space in $spacesInAll )
## check if current user can view space
#if ( $permissionHelper.canView( $action.getAuthenticatedUser(), $space ) )
<li><a href="$!space.urlPath">$!space.name</a></li>
#end
#end
</ul>
#set ( $d = "$") ## escape the dollar sign for jQuery in velocity
<script>
## Sort the list client side as this can't be done server side in a user macro.
AJS.toInit(function (${d}) {
${d}('.spaces-by-category-user-macro').each(function(){
var ${d}me = ${d}(this);
// Sort the list items
var listItems = ${d}me.children('li').get();
listItems.sort(function(x,y) {
var compareX = ${d}(x).text().toUpperCase();
var compareY = ${d}(y).text().toUpperCase();
return (compareX < compareY) ? -1 : (compareX > compareY) ? 1 : 0;
});
// Write the list items back to the DOM
${d}.each(listItems, function(index, item) { ${d}me.append(item); });
});
});
</script>
#end
好的,我找到了一个 "workaround" 我认为...或解决方案...但对我来说,它似乎不仅仅是一种解决方法:
如果将 .add()
方法封装在 #set()
方法中,您可以将方法的输出(true
)重定向到一个变量中,以防止它被打印出来。
所以调用应该是这样的:
#set($result = $list.add($whatsoever))
我也 运行 遇到了这个问题,并试图提出一个解决方案来避免将 #set()
与虚拟变量一起使用。似乎在 Confluence 中 .add()
的输出必须封装在更多 Velocity 代码中才能不呈现。
这是我能想到的最简单的解决方案,但它仍然让我想挖出我的眼睛:
#if( $array.add($data) ) #end