【问题标题】:How does the @ option operator work in AEM HTL templates@ 选项运算符如何在 AEM HTL 模板中工作
【发布时间】:2019-07-16 12:21:19
【问题描述】:

我花了一些时间尝试阅读 AEM 上的 official documentation @ 运算符和 official syntax 规范,但我不明白背后的逻辑。

让我们以文档中的这个例子为例:

${myVar @ optOne, optTwo=bar}

如何计算这个表达式?赋值optTwo=bar有什么效果?

或者语法规范中的这个例子:

${myVar @ optOne, optTwo=myVar, optThree='string', optFour=[myVar, 'string']}

列表中的每个赋值 (optTwo=myVar, optThree='string', optFour=[myVar, 'string']) 将如何影响表达式的最终值?

【问题讨论】:

    标签: syntax operators aem


    【解决方案1】:

    在我的项目中,我们使用这个 @ 运算符将值传递给 Java 后端逻辑。我创建了一个示例用例来演示 @ 运算符的用法。

    我创建了一个带有文本字段、数字字段和复选框的简单信息组件。对应的cq:dialog的.content.xml如下-

    <?xml version="1.0" encoding="UTF-8"?>
    <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
        jcr:primaryType="nt:unstructured"
        jcr:title="Info"
        sling:resourceType="cq/gui/components/authoring/dialog">
        <content
            jcr:primaryType="nt:unstructured"
            sling:resourceType="granite/ui/components/foundation/container">
            <layout
                jcr:primaryType="nt:unstructured"
                sling:resourceType="granite/ui/components/foundation/layouts/tabs"
                type="nav"/>
            <items jcr:primaryType="nt:unstructured">
                <tab
                    jcr:primaryType="nt:unstructured"
                    jcr:title="Properties"
                    sling:resourceType="granite/ui/components/foundation/container">
                    <items jcr:primaryType="nt:unstructured">
                        <columns
                            jcr:primaryType="nt:unstructured"
                            sling:resourceType="granite/ui/components/foundation/container">
                            <items jcr:primaryType="nt:unstructured">
                                <name
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/textfield"
                                    class="field-whitespace"
                                    fieldDescription="Enter full name of the user"
                                    fieldLabel="Full Name"
                                    name="./fullName"/>
                                <age
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/numberfield"
                                    class="field-whitespace"
                                    fieldDescription="Enter the age of the user"
                                    fieldLabel="Age"
                                    name="./age"/>
                                <married
                                    jcr:primaryType="nt:unstructured"
                                    sling:resourceType="granite/ui/components/foundation/form/checkbox"
                                    class="field-whitespace"
                                    fieldDescription="Check if the user is married"
                                    name="./married"
                                    text="Married?"
                                    value="true"/>
                            </items>
                        </columns>
                    </items>
                    <layout
                        jcr:primaryType="nt:unstructured"
                        sling:resourceType="granite/ui/components/foundation/layouts/fixedcolumns"/>
                </tab>
            </items>
        </content>
    </jcr:root>
    

    对应的HTL文件如下-

    <div data-sly-test="${wcmmode.edit || wcmmode.design}">
        Info Component
    </div>
    
    <sly
        data-sly-use.info="${'org.redquark.aem.learning.core.cq.text.InfoComponent' @
            fullName=properties.fullName,
            age=properties.age,
            married=properties.married
        }" />
    
    <sly data-sly-test="${info}">
        <h1>${info.details}</h1>
    </sly>
    

    在这里,您可以看到,在标记中,我们在变量 fullNameagema​​rried 中传递来自 JCR 的值。

    读取这些值的java代码如下-

    package org.redquark.aem.learning.core.cq.text;
    
    import com.adobe.cq.sightly.WCMUsePojo;
    
    public class InfoComponent extends WCMUsePojo {
    
        private String details;
    
        @Override
        public void activate() throws Exception {
    
            String fullName = get("fullName", String.class);
            Integer age = Integer.parseInt(get("age", String.class));
            Boolean isMarried = Boolean.parseBoolean(get("married", String.class));
    
            details = fullName + " is " + age + " years old and is " + (isMarried ? "married" : " not married");
        }
    
        /**
         * @return the details
         */
        public String getDetails() {
            return details;
        }
    }
    

    或者,如果您使用的是 SlingModels 而不是 Java Use API,那么您可以以典型的 Sling Model 方式访问这些值。

    package org.redquark.aem.learning.core.models.text;
    
    import javax.annotation.PostConstruct;
    import javax.inject.Inject;
    import javax.inject.Named;
    
    import org.apache.sling.api.resource.Resource;
    import org.apache.sling.models.annotations.Default;
    import org.apache.sling.models.annotations.DefaultInjectionStrategy;
    import org.apache.sling.models.annotations.Model;
    import org.apache.sling.models.annotations.Optional;
    
    
    @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
    public class InfoModel {
    
        @Inject
        @Optional
        @Default(values = "Test")
        private String fullName;
    
        @Inject
        @Optional
        @Default(values = "0")
        private String age;
    
        @Inject
        @Optional
        @Named("married")
        @Default(values = "false")
        private String isMarried;
    
        // Variable that will be read in the HTL file
        private String details;
    
        @PostConstruct
        protected void init() {
    
            details = fullName + " is " + Integer.parseInt(age) + " years old and is "
                    + (Boolean.parseBoolean(isMarried) ? "married" : " not married");
        }
    
        /**
         * @return the details
         */
        public String getDetails() {
            return this.details;
        }
    
    }
    

    当然,那你必须在 HTL 代码中调用你的 InfoModel 类

    <sly data-sly-use.info="${'org.redquark.aem.learning.core.models.text.InfoModel' @
            fullName=properties.fullName,
            age=properties.age,
            married=properties.married
        }" />
    

    您看,这里我们正在读取在 HTL 代码中传递的相同值。然后我们可以对它们执行任何业务逻辑。

    我希望这会有所帮助。干杯!

    【讨论】:

    • 吊索模型如何发挥作用?
    • @Dileepa,我已经更新了我对 Sling Model 实施的回答。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    相关资源
    最近更新 更多