NetSuite FreeMarker Advanced PDF HTML 模板 IF ELSE 错误
NetSuite FreeMarker Advanced PDF HTML Template IF ELSE Error
我有以下 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} ${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> </#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> </#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.
有人知道我做错了什么或者我该如何解决这个问题吗?
既然你说空率你可以使用??测试运算符:
<#if item.rate??></#if>
此外,如果您想检查多个条件,您可以检查 <#if item.rate?? && item.rate?has_content></#if>
货币格式可以参考这个linkdocs . If you want custom format you can do try this
${item.rate:M2}
Freemarker ?has_content
函数可能会产生意想不到的结果 - 特别是当您不控制底层数据模型时(NetSuite 就是这种情况)。本质上发生的事情是传递的 item.rate
不是 null
并且不符合 ?has_content
对 "empty" 的定义,即使它看起来是空的。您可以使用 ?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> </#if></td>
这仍然是一个问题,例如,如果 item.rate
出于某种原因传递给没有“.”的模板,因为它仍然会以空字符串结尾,为此[0..1]
索引无效。
因此,您可以测试 item.rate
?contains
是否是小数点分隔符:
<td align="right" colspan="4"><#if item.rate?contains(".")>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else> </#if></td>
但最简单的方法可能是使用 Freemarker 的内置货币字符串格式:
<td align="right" colspan="4">${item.rate?string.currency}</td>
我有以下 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} ${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> </#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> </#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.
有人知道我做错了什么或者我该如何解决这个问题吗?
既然你说空率你可以使用??测试运算符:
<#if item.rate??></#if>
此外,如果您想检查多个条件,您可以检查 <#if item.rate?? && item.rate?has_content></#if>
货币格式可以参考这个linkdocs . If you want custom format you can do try this
${item.rate:M2}
Freemarker ?has_content
函数可能会产生意想不到的结果 - 特别是当您不控制底层数据模型时(NetSuite 就是这种情况)。本质上发生的事情是传递的 item.rate
不是 null
并且不符合 ?has_content
对 "empty" 的定义,即使它看起来是空的。您可以使用 ?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> </#if></td>
这仍然是一个问题,例如,如果 item.rate
出于某种原因传递给没有“.”的模板,因为它仍然会以空字符串结尾,为此[0..1]
索引无效。
因此,您可以测试 item.rate
?contains
是否是小数点分隔符:
<td align="right" colspan="4"><#if item.rate?contains(".")>${item.rate?keep_before_last(".")}.${item.rate?keep_after_last(".")[0..1]}<#else> </#if></td>
但最简单的方法可能是使用 Freemarker 的内置货币字符串格式:
<td align="right" colspan="4">${item.rate?string.currency}</td>