在Velocity模板中使用动态变量

时间:2017-10-19 16:28:50

标签: velocity

我正在使用速度模板来生成电子邮件模板,在这里我想为每个标签创建表格'将各自的值作为表格数据。

我正在传递一个List标签,其中包含标签的详细信息,包括tagName,我正在为每个标签传递相应的List。 (如果tagName =" test",我将测试表的数据作为$ test传递)

如图所示,我已将每个表的数据设置为$ arrayobjs,但该表不显示行,因为我认为它不会遍历$ arrayobs。请帮我迭代数据并将数据设置为coloumns。

P.S.-虽然我能够打印$ tags [0]的值 ,$ arrayobjs [0]不打印值

    #set ($d = '$')                
        #foreach($tag in $tags)
        #set ($var = "${d}${tag.tagName}")
        #set ($arrayobjs = "#evaluate($var)")
            <table style='width:600px; padding-left: 11px; font:Segoe UI;'>
                <tr>
                    <td> <span style='font-size: 17px; font-weight: 400; font-family:Georgia;'>$tag.tagName </span></td>
                </tr>
                </table>
                <br>
                <table width=583 style='width:437.5pt; margin-left:8.25pt;border-collapse:collapse;font-family:"Segoe UI",sans-serif;'>
                    <tr  style='vertical-align:top;height:10px;'>

                        <td width=50px style='border:solid white 1.0pt;background: #a3a3c2;padding:1pt 1pt 1pt 1pt;'>
                            <p> <span style='font-size:10.0pt;color:#3d3d5c;'><b>COL1</b></span> <span style='font-size:9.0pt;color:#404040'> <br><b></b> </span> </p>
                        </td>
                        <td width=100px style='border:solid white 1.0pt;background: #a3a3c2;padding:1pt 1pt 1pt 1pt;'>
                            <p> <span style='font-size:10.0pt;color:#3d3d5c'><b>COL2</b></span> <span style='font-size:9.0pt;color:#404040'> <br><b></b> </span> </p>
                        </td>
                        <td width=50px style='border:solid white 1.0pt;background: #a3a3c2;padding:1pt 1pt 1pt 1pt;'>
                            <p> <span style='font-size:10.0pt;color:#3d3d5c'><b>COL3</b></span> <span style='font-size:9.0pt;color:#404040'> <br><b></b> </span> </p>
                        </td>
                        <td width=50px style='border:solid white 1.0pt;background: #a3a3c2;padding:1pt 1pt 1pt 1pt;'>
                            <p> <span style='font-size:10.0pt;color:#3d3d5c'><b>COL4</b></span> <span style='font-size:9.0pt;color:#404040'> <br><b></b> </span> </p>
                        </td>
                        <td width=50px style='border:solid white 1.0pt;background: #a3a3c2;padding:1pt 1pt 1pt 1pt;'>
                            <p> <span style='font-size:10.0pt;color:#3d3d5c'><b>COL5</b></span> <span style='font-size:9.0pt;color:#404040'> <br><b></b> </span> </p>
                        </td>
                        <td width=50px style='border:solid white 1.0pt;background: #a3a3c2;padding:1pt 1pt 1pt 1pt;'>
                            <p> <span style='font-size:10.0pt;color:#3d3d5c'><b>COL6</b></span> <span style='font-size:9.0pt;color:#404040'> <br><b></b> </span> </p>
                        </td>
                    </tr>
                    #foreach($o in $arrayobjs)
                    <tr  style='vertical-align:top;height:5px;'>
                        <td width=50px style='border:solid white 1.0pt;background: #f0f0f5;padding:0.5pt 0.5pt 0.5pt 0.5pt;'>
                            <p> <span style='font-size:9.0pt;color:#7F7F7F'></span> <span style='font-size:9.0pt;color:#404040'>$o.col1 </span> </p>
                        </td>
                        <td width=180px style='border:solid white 1.0pt;background: #f0f0f5;padding:0.5pt 0.5pt 0.5pt 0.5pt;'>
                            <p> <span style='font-size:9.0pt;color:#7F7F7F'></span> <span style='font-size:9.0pt;color:#404040'>$o.col2</span> </p>
                        </td>
                        <td width=30px style='border:solid white 1.0pt;background: #f0f0f5;padding:0.5pt 0.5pt 0.5pt 0.5pt;'>
                            <p> <span style='font-size:9.0pt;color:#7F7F7F'></span> <span style='font-size:9.0pt;color:#404040'>$o.col3</span> </p>
                        </td>
                        <td width=30px style='border:solid white 1.0pt;background: #f0f0f5;padding:0.5pt 0.5pt 0.5pt 0.5pt;'>
                            <p> <span style='font-size:9.0pt;color:#7F7F7F'></span> <span style='font-size:9.0pt;color:#404040'>$o.col4</span> </p>
                        </td>
                        <td width=30px style='border:solid white 1.0pt;background: #f0f0f5;padding:0.5pt 0.5pt 0.5pt 0.5pt;'>
                            <p> <span style='font-size:9.0pt;color:#7F7F7F'></span> <span style='font-size:9.0pt;color:#404040'>$o.col5</span> </p>
                        </td>
                        <td width=30px style='border:solid white 1.0pt;background: #f0f0f5;padding:0.5pt 0.5pt 0.5pt 0.5pt;'>
                            <p> <span style='font-size:9.0pt;color:#7F7F7F'></span> <span style='font-size:9.0pt;color:#404040'>$o.col6</span> </p>
                        </td>
                    </tr>
                    #end
                </table>
            #end

1 个答案:

答案 0 :(得分:0)

将近2年后,我提出以下答案:-)

#set ($d = '$')
#set ($h = '#')

#foreach($tag in $tags)
    #set ( $temp = "${h}set ( ${d}var = ${d}${tag.tagName} )" )
    #evaluate($temp)

    ## html code for table header

    #foreach($o in $var)
        ## table rows
    #end
#end