【问题标题】:Confluence Macro: Velocity prints out unwanted "true" when using Array.add()Confluence 宏:使用 Array.add() 时,Velocity 会打印出不需要的“true”
【发布时间】: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


    【解决方案1】:

    好的,我找到了我认为的“解决方法”……或者解决方案……但对我来说,这似乎不仅仅是一种解决方法:

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

    所以调用看起来像这样:

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

    【讨论】:

    • 这解决了我的问题,但我不禁觉得可能有更好的解决方案。
    【解决方案2】:

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

    这是我能想到的最简单的解决方案,但它仍然让我想睁大眼睛:

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

    【讨论】:

      猜你喜欢
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      相关资源
      最近更新 更多