【问题标题】:HTL AEM conditional OR operator issueHTL AEM 条件 OR 运算符问题
【发布时间】:2020-07-23 23:46:08
【问题描述】:

我希望能够使用“||”上下文中的 OR 运算符

在架构中,如果字段中的值不是作者,我需要从 JCR 内容中提取两个日期

 <script type="application/ld+json" >
"datePublished": "${properties.datePublishedArticle @context="html" ||'yyyy-MM-dd' @ format=currentPage.lastModified }",
"dateModified": "${properties.dateModifiedArticle @ context="html" || 'yyyy-MM-dd' @ format=currentPage.lastModified}"
</script>

org.apache.sling.api.scripting.ScriptEvaluationException:

mismatched input '@' expecting {'}', '.', 'in', '&&', '||', ',', '['} in line 67 where datepublished is located. 


换句话说,如果作者没有编写值,它将从 jcr 内容中获取值。单独完成时它们工作正常。 不明白所指示的错误。

【问题讨论】:

    标签: aem sightly


    【解决方案1】:

    你有没有试过这样使用:

    <script type="application/ld+json" >
        "datePublished": "${properties.datePublishedArticle ||'yyyy-MM-dd' @ context="html", format = currentPage.lastModified }",
        "dateModified": "${properties.dateModifiedArticle || 'yyyy-MM-dd' @ context="html", format = currentPage.lastModified}"
    </script>
    

    我认为错误正在发生,因为您不应该在同一个 HTL 语句中重复“@”块。

    【讨论】:

    • 成功了!在同一个语句中没有看到@有问题
    【解决方案2】:

    虽然@atgar 的解决方案没有抛出错误,但我认为它并没有按照作者的要求:输出作者日期或JCR (lastModified) 日期。

    这样的东西会更接近他的需要:

    <script type="application/ld+json" >
        "datePublished": "${'yyyy-MM-dd' @ context='html', format = (properties.datePublishedArticle ? properties.datePublishedArticle : currentPage.lastModified) }",
        "dateModified": "${'yyyy-MM-dd' @ context='html', format = (properties.dateModifiedArticle ? properties.dateModifiedArticle : currentPage.lastModified)}"
    </script>
    

    请注意,使用HTL date formatting时,实际值应在format选项中。

    【讨论】:

    • 在 HTL "||"如果第一个值为空,则 OR 运算将采用第二个值。
    • 例如当前作者没有手动输入日期值时,HTL会自动更新HTML以显示jcr值,但是一旦对话框中有值字段,那么value 将覆盖 JCR 值。
    猜你喜欢
    • 1970-01-01
    • 2010-12-20
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2011-02-21
    • 2013-06-20
    相关资源
    最近更新 更多