使用Freemarker将字符串转换为布尔值

时间:2018-04-05 15:35:45

标签: boolean freemarker

如何转换字符串" true"布尔值true和"假"到布尔false

我用这种方式定义了一些链接:

<#assign mailtoLink  =(setting(self, 'my.link.mailto'))! />
...

<#assign links = [
            {"linkTarget":mailtoLink,   "linkText":mailtoService,   "externalLink":"false", "iconName":"#icon-email"   },
            {"linkTarget":facebookLink, "linkText":facebookService, "externalLink":"true", "iconName":"#icon-facebook" }, ...]/>

内容来自CMS。 然后我想使用循环将它们显示在列表中,其中链接的属性将用作宏的参数:

<ul class="my-options-nav__list"> 
            <#list links as link>
                <#if link.linkTarget?has_content>
                    <li class="bwr-options-nav__list-item">
                       <@m.link
                            linkTarget=link.linkTarget
                            linkText=link.linkText
                            externalLink=link.externalLink?boolean
                            linkClass="bwr-link"
                            iconLeft={"id": "${link.iconName}"}
                        />
                    </li>
                </#if>
            </#list>
        </ul>

我总是从CoreMedia(或Freemarker?)获得例外:

model   

com.coremedia.blueprint.cae.contentbeans.CMChannelImpl$$[id=127994]
view name   m.asOptionsNav
view    FreemarkerView[template=//WEB-INF/templates/sites/bwr17/com.coremedia.blueprint.common.contentbeans/CMChannel.m.asOptionsNav.ftl]
cause   Error rendering view
Error on line 54, column 80, in template /WEB-INF/templates/sites/com.coremedia.blueprint.common.contentbeans/CMChannel.m.asOptionsNav.ftl Found boolean, expecting one of: chunk, is_date, is_hash, float, matches, time, number_to_datetime, byte, substring, is_transform, web_safe, iso_ms_nz, groups, seq_contains, iso, is_method, eval, iso_ms, iso_utc_m_nz, parent, capitalize, number, if_exists, is_directive, iso_utc_h_nz, floor, is_boolean, split, node_name, seq_index_of, is_sequence, sort, is_node, sort_by, xhtml, iso_local_m_nz, iso_nz, node_namespace, date, last_index_of, short, ancestors, length, datetime, iso_local_ms, json_string, reverse, c, keys, iso_local_h_nz, has_content, replace, is_hash_ex, is_number, new, lower_case, string, exists, last, root, j_string, contains, round, long, ends_with, number_to_date, namespace, values, seq_last_index_of, uncap_first, iso_local, is_macro, index_of, word_list, int, iso_utc_nz, xml, double, node_type, rtf, url, iso_m, size, default, iso_h, iso_utc_m, ceiling, iso_h_nz, is_enumerable, iso_utc_h, iso_local_nz, iso_m_nz, left_pad, iso_utc_ms_nz, cap_first, interpret, children, chop_linebreak, is_collection, iso_utc, trim, is_string, number_to_time, upper_case, js_string, right_pad, is_indexable, iso_local_ms_nz, iso_utc_ms, iso_local_h, html, iso_local_m, first, starts_with in /WEB-INF/templates/sites/com.coremedia.blueprint.common.contentbeans/CMChannel.m.asOptionsNav.ftl

1 个答案:

答案 0 :(得分:1)

问题在于您没有将布尔值转换为计算机可读格式。

  

第54行,第80列,模板中的错误 [...] CMChannel.m.asOptionsNav.ftl 找到布尔值

来自doc

See ?c

  

这个内置函数将一个布尔值转换为一个&#34;计算机语言的字符串&#34;而不是人类观众。 [...] 生成JavaScript等时,应该使用

意思是你必须使用

externalLink=(link.externalLink?boolean)?c

以生成externalLink=trueexternalLink=false