Search code examples
javaadobeaemsightly

Usage of HTL data-sly-use?


I've seen different usage for HTL data-sly-use

I've seen it this way:

<sly data-sly-use.example="com.example.aem.HellowWorldModel"> 
 <!--/* all code here */--> 
</sly>

And I've seen it this way self closing:

<sly data-sly-use.example="com.example.aem.HellowWorldModel" />
<!--/* All code after */-->

Why is each way used and what is the best way to use this?


Solution

  • I don't think there is any defined standard as to which way to use it. It might completely come down to individual preferences, as both the approaches would make the use object available to the entire template.

    You can notice both the approaches being used in the official documentation as well.

    Self-closing tag approach in the relational operations section of the docs

    <sly data-sly-use.logic="logic.js" />
    ${'a' in logic} <!--/* returns true */-->
    ${'b' in logic} <!--/* returns true */-->
    ${'c' in logic} <!--/* returns true */-->
    ${'two' in logic} <!--/* returns false */-->
    

    and the other approach in the rest of the documentation.

    Using the self-closing tag approach would help avoid extra indentation of the file contents, whereas the nested tag approach can help with code readability, especially if you have multiple use objects within the template and want to segregate the code into blocks within which the object might be used.