NetSuite FreeMarker Advanced PDF HTML模板IF ELSE错误

时间:2019-02-25 11:20:24

标签: netsuite freemarker

我有以下NetSuite Advanced PDF HTML模板代码给我一个错误:

<#if record.item?has_content>

<table class="itemtable" style="width: 100%;"><!-- start items --><#list record.item as item><#if item_index==0>
<thead>
  <tr>
  <th colspan="4">Item Code</th>
  <th colspan="12">Item Description</th>
  <th align="right" colspan="2">UOM1</th>
  <th align="right" colspan="3">${item.quantity@label}</th>
  <th align="right" colspan="3">UOM2</th>
  <th align="right" colspan="4">Unit Price (excl. VAT)</th>
  <th align="right" colspan="3">${item.amount@label}</th>
  </tr>
</thead>
</#if><tr>
  <td colspan="4">${item.item}</td>
  <td colspan="12">${item.description}</td>
  <td align="right" colspan="2">${item.custcolsyn_uom}&nbsp;${item.custcolsyn_unit_measure}</td>
  <td align="right" colspan="3">${item.quantity}</td>
  <td align="right" colspan="3">${item.units}</td>
  <td align="right" colspan="4"><#if item.rate?has_content>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>
  <td align="right" colspan="3">${item.amount}</td>
  </tr>
  </#list><!-- end items --></table>
</#if>

问题出在那一行:

<td align="right" colspan="4"><#if item.rate?has_content>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

FreeMarker似乎正在评估以下部分

${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}

即使该订单项没有任何费率。当然

<#if item.rate?has_content>

应避免进行该评估。我试图只保留2位小数的货币数据,而我尝试的所有其他方法都丢失了货币符号。

我们正在使用NetSuite的最新版本(2018.2)。

错误消息是:

The template cannot be printed due to the following errors: 

Error on line 239, column 95 in template.

Detail...

Range start index 0 is out of bounds, because the sliced string has only 0 character(s). (Note that indices are 0-based).
The blamed expression:
==> 0..1 [in template "template" at line 239, column 128]

----
FTL stack trace ("~" means nesting-related):
- Failed at: ${item.rate?keep_after_last(".")[0..1]} [in template "template" at line 239, column 95]
----


Please contact your administrator.

有人对我做错了什么或如何解决这个问题有任何想法吗?

2 个答案:

答案 0 :(得分:3)

Freemarker ?has_content函数会产生意想不到的结果-尤其是当您不控制基础数据模型时(NetSuite就是这种情况)。本质上发生的是,传递的item.rate不是null,即使它看起来是空的,也不符合?has_content对“空”的定义。您可以改用?length gt 0

  <td align="right" colspan="4"><#if item.rate?length gt 0>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

例如,如果出于某种原因将item.rate传递给模板时不带“。”,这仍然是一个问题,因为它最终仍以空字符串结尾,为此{ 1}}索引将无效。

因此,您可以测试[0..1] item.rate是否用小数点分隔符代替:

?contains

但是最简单的方法可能是使用Freemarker的内置货币字符串格式代替:

<td align="right" colspan="4"><#if item.rate?contains(".")>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else>&nbsp;</#if></td>

答案 1 :(得分:1)

由于您说的是null,您可以使用??。测试人员:

<#if item.rate??></#if>

如果您要检查多个条件,也可以检查<#if item.rate?? && item.rate?has_content></#if>

有关货币格式,您可以参考此链接docs。如果您想要自定义格式,可以尝试使用this

${item.rate:M2}