【发布时间】:2016-03-07 20:36:40
【问题描述】:
现在我正在为 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
【问题讨论】:
标签: macros velocity confluence