Confluence Macro:使用Array.add()时Velocity打印出不需要的“true”

时间:2015-12-03 09:40:01

标签: macros velocity 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

2 个答案:

答案 0 :(得分:3)

好的,我找到了一个&#34;解决方法&#34;我认为......或解决方案......但对我而言,这似乎不仅仅是一种解决方法:

如果您将.add()方法封装在#set()方法中,则可以将方法的输出(true)重定向到一个变量,以防止它被打印出来。

所以电话会是这样的:

#set($result = $list.add($whatsoever))

答案 1 :(得分:2)

我也遇到了这个问题,并尝试提出一个解决方案,以避免使用#set()和虚拟变量。似乎在Confluence中,.add()的输出必须封装在更多的Velocity代码中才能呈现。

这是我能提出的最简单的解决方案,但它仍然让我想要抓住我的眼睛:

#if( $array.add($data) ) #end