Search code examples
aemsightly

HTL AEM conditional OR operator issue


I want to be able to use the "||" OR operator in this context

inside a schema, i have two dates I need to pull from the JCR content if the value in the field has not been the author

 <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. 


In order words, if author has not authored a value, it will take the value from the jcr content. They work fine when done separately. Do not understand the error that is indicating.


Solution

  • Have you tried to use it like this:

    <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>
    

    I would think that the error is happening because you shouldn't be repeating the '@' block in the same HTL statement.