Calling Macro function in Velocity template

I am trying to figure out how to return a value from a velocity macro call and assign it to a varaible

my macro function looks something like this. its once in common shared macros files

#macro(getBookListLink, $readingTrackerResult)
   $readingTrackerResult.getBookListLink()
#end

I am need to assign the result of this macro to a variable in another velocity template file

I tried something like this

#set($book_list_link = #getBookListLink( $readingTrackerResult ))

but did not work. I tried with #,$ and with nothing in front of function getBookListLink. but nothing worked. Can not i return from a macro? something wrong with my macro?

But, As such if i call #getBookListLink( $readingTrackerResult ) separately in html file. it works and i can print the result to UI. But not able to assign to a variable.


Macros are not functions; they are for rendering output. However, if you don't mind losing the type and getting the result as text...

#set( $book_list_link = "#getBookListLink( $readingTrackerResult )" )

为了消除空格和空行,使用多行注释( #* comment *# ):

#macro( myMacro $param )#*
  *#the_return_value#*
*##end

而不是生活在'返回值'的字符串限制,最好外部定义的结果变量可以'通过引用'传递,例如:

#macro(getBookListLink $inTrackerResult $outBookListLink)
    #if ($outBookListLink)
        #set ($outBookListLink = $inTrackerResult.getBookListLink())
    #end
#end

#set ($myLink = "")
#getBookListLink($myTrackerResult $myLink)
myBookListLink = "$myLink"<br/>
链接地址: http://www.djcxy.com/p/47312.html

上一篇: 从速度模板获取参数

下一篇: 在速度模板中调用宏函数