【问题标题】:How to compare two strings in Sightly/HTL? (AEM 6.2)如何在 Sightly/HTL 中比较两个字符串? (AEM 6.2)
【发布时间】:2017-07-24 02:57:29
【问题描述】:

我创建了一个简单的下拉菜单,其中包含两个选项 Yes 和 No,它们分别返回“true”和“false”的值。

<enableTool
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/select"
class="cq-dialog-dropdown-showhide"
cq-dialog-dropdown-showhide-target=".list-option-listfrom-showhide-target"
fieldLabel="Enable tooltip?"
name="./enableTool">
    <items jcr:primaryType="nt:unstructured">
        <true
            jcr:primaryType="nt:unstructured"
            text="Yes"
            value="true"/>
        <false
            jcr:primaryType="nt:unstructured"
            text="No"
            value="false"/>
    </items>
</enableTool>

我想直观地实现以下逻辑

if (value of the returned string is true)
{
Do something
}

为此,写了如下漂亮的代码

<div data-sly-test="${properties.enableTool == 'true'}"> Value is true</div>

当我运行此“值为真”时,无论propeties.enableTool 的值是真还是假,总是会打印出来。

请注意,如果我写 &lt;div&gt; ${properties.enableTool} &lt;/div&gt; ,我会根据我的选择正确地将值视为 true 或 false 。

知道我做错了什么吗?

提前致谢!

【问题讨论】:

  • 我没有看到您的代码有问题。它应该可以正常工作。为了更好地调试,请添加更多与对话框和 HTL 文件相关的代码。
  • 我使用提供的代码创建了一个字段,并使用相同的代码来检查测试条件,它对我来说工作正常,我使用的 Sightly 代码:
    值为真
    值为假
  • 和上面的两个 cmets 一样,我没有在您的代码中看到问题,您使用的是什么版本的 AEM?另外,我建议您使用 HTL REPL 进行快速 HTL 测试:github.com/Adobe-Marketing-Cloud/aem-htl-repl

标签: aem sightly htl


【解决方案1】:

我相信HTL Conditional Ternary operator 是您正在寻找的。​​p>

<p>${properties.enableTool ? "tool is enabled" : "tool is disabled"}</p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 2022-06-29
    相关资源
    最近更新 更多