0

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

在对话框中

<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}">
4

1 回答 1

2

first, your dialog has name="./number and in your HTL you use properties.podnumber they do not match.

To answer your question: there is no way to do this with sightly alone, the context option is only for rendering (XSS protection) and does not change the value.

Your best bet is to use a sling model, something like

I assume your dialog will have name="podNumber"

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

  @Inject
  int getPodNumber();

}

Sling will then convert that value to an integer you can use in your comparison. so you can add your model with data-sly-use.myModel="package.name.MyModel" then use it:

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

By the way, all the values in your dropdown are larger than or equal to 1.

NOTE: as Florian suggested in the comment below, you should use boolean checks in the model, instead of having to compare values in HTL. for example:

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

  @Inject
  int podNumber;

  boolean isLargerThanOne(){
     return podNumber > 1;
  }
于 2018-08-24T22:14:09.740 回答