可视地比较对话框中的数字和值

时间:2018-08-24 19:15:48

标签: html adobe aem sightly htl

我希望能够读取在对话框中设置的值,并在Sightly中使用它来控制显示代码的哪一部分。当我尝试使用下面的代码时,我收到此错误“操作数不是同一类型:仅对数字支持比较”。 我尝试了许多不同的修复程序,但没有发现任何可行的方法或任何文档。上下文='数字'是不正确的语法还是我需要添加的其他内容?

在对话中

<number
      jcr:primaryType="nt:unstructured"
      sling:resourceType="granite/ui/components/foundation/form/select"
      fieldLabel="Select Amount of Delivery Options"
      name="./number"
      value = "4" >
      <items jcr:primaryType="nt:unstructured">
          <four
               jcr:primaryType="nt:unstructured"
               text="Four"
               value= "4" />
         <three
               jcr:primaryType="nt:unstructured"
               text="Three"
               value= "3" />
         <two
             jcr:primaryType="nt:unstructured"
             text="Two"
             value= "2" />
         <one
             jcr:primaryType="nt:unstructured"
             text="One"
             value= "1" />
 </items> </number>   

在HTL中

<sly data-sly-test="${properties.podnumber @ context = 'number' >= 1}">

1 个答案:

答案 0 :(得分:2)

首先,您的对话框包含name="./number,在您的HTL中使用properties.podnumber,它们不匹配。

要回答您的问题:仅凭表面上的方法就无法做到这一点,context选项仅用于渲染(XSS保护),并且不会更改值。

您最好的选择是使用吊索模型,例如

  

我认为您的对话框将包含name="podNumber"

@Model(
    adaptables = {Resource.class},
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public interface MyModel {

  @Inject
  int getPodNumber();

}

Sling会将其转换为可以在比较中使用的整数。因此您可以使用data-sly-use.myModel="package.name.MyModel"添加模型,然后使用它:

<sly data-sly-test="${myModel.podNumber >= 1}">

  

顺便说一下,下拉菜单中的所有值都大于或等于1。

注意::如Florian在下面的注释中建议的那样,您应该在模型中使用布尔检查,而不必在HTL中比较值。例如:

@Model(
    adaptables = {Resource.class},
    defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class MyModel {

  @Inject
  int podNumber;

  boolean isLargerThanOne(){
     return podNumber > 1;
  }