Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/netcentric/aem-htl-style-guide
A style guide for the HTML Template Language (HTL), the templating language use by the Adobe Experience Manager (AEM).
https://github.com/netcentric/aem-htl-style-guide
aem htl sightly
Last synced: 6 days ago
JSON representation
A style guide for the HTML Template Language (HTL), the templating language use by the Adobe Experience Manager (AEM).
- Host: GitHub
- URL: https://github.com/netcentric/aem-htl-style-guide
- Owner: Netcentric
- License: mit
- Created: 2015-07-26T18:41:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-05-30T10:46:32.000Z (8 months ago)
- Last Synced: 2024-11-13T08:38:28.262Z (2 months ago)
- Topics: aem, htl, sightly
- Homepage:
- Size: 127 KB
- Stars: 139
- Watchers: 49
- Forks: 51
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AEM HTL Style Guide
A style guide for the [HTML Template Language](https://docs.adobe.com/docs/en/htl.html) (HTL), formerly known as Sightly, the HTML templating system from Adobe Experience Manager (AEM).## Table of Contents
1. [HTML](#html)
2. [Comments](#comments)
3. [Expression language](#expression-language)
4. [Block statements](#block-statements)- [1.1](#1.1) **Avoid inline JavaScript or CSS.**
In order to encourage keeping a clean separation of concerns, HTL has by design some limitations for inline JavaScript or CSS. First, because HTL doesn't parse JavaScript or CSS, and therefore cannot automatically define the corresponding escaping, all expressions written there must provide an explicit `context` option. Then, because the HTML grammar ignores elements located inside a `` or `<style>` elements, no block statement can be used within them.
Therefore JavaScript and CSS code should instead be placed into corresponding `.js` and `.css` files. Data attributes are the easiest way to communicate values to JavaScript, and class names are the best way to trigger specific styles.```html
<!--/* Bad */-->
<section data-sly-use.teaser="com.example.TeaserComponent" class="teaser">
<h2 class="teaser__title">${teaser.title}</h2>
<script>
var teaserConfig = {
skin: "${teaser.skin @ context='scriptString'}",
animationSpeed: ${teaser.animationSpeed @ context='number'}
};
.teaser__title {
font-size: ${teaser.titleFontSize @ context='styleToken'}
}
${teaser.title}
```**[⬆ back to top](#table-of-contents)**
- [2.1](#2.1) **Use HTL comments.**
Normal HTML comments get rendered to the final markup. To keep the DOM clean, always use HTL comments over normal HTML comments.```html
```**[⬆ back to top](#table-of-contents)**
- [3.1](#3.1) **Set a display context only if necessary**
In most cases you can leave out the display context, because it is determined automatically.- [3.2](#3.2) **Use the safest possible display context.**
From the following list of contexts, always choose the one closest to the top that fits your needs:
`number`: For whole numbers (in HTML, JS or CSS)
`uri`: For links and paths (in HTML, JS or CSS, applied by default for `src` and `href` attributes)
`elementName`: For HTML element names (applied by default by `data-sly-element`)
`attributeName`: For HTML attribute names (applied by default by `data-sly-attribute` for attribute names)
`scriptToken`: For JavaScript identifiers and keywords
`styleToken`: For CSS identifiers and keywords
`scriptString`: For text within JavaScript strings
`styleString`: For text within CSS strings
`attribute`: For HTML attribute values (applied by default for attribute values)
`text`: For HTML text content (applied by default for any content)
`html`: For HTML markup (it filters out all elements and attributes that could be dangerous)
`unsafe`: Unescaped and unfiltered direct output```html
${teaser.title}
${teaser.htmlContent @ context='unsafe'}
${teaser.title}
${teaser.htmlContent @ context='html'}
```- [3.3](#3.3) **Avoid writing unnecessary expressions for literals.**
It might sound obvious, but an expression with just a string literal inside equals just that string literal.```html
...
...
```
- [3.4](#3.4) **Avoid using the ternary operator unnecessarily.**Take advantage of the logical `||` operator to simplify your code.
```html
```- [3.5](#3.5) **Use the native URI manipulation capabilities of HTL.**
Rolling out a custom URI builder is error prone and hardcoding URL's is even worse. Use [HTL URI Manipulation](https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#125-uri-manipulation) instead, in particular, the `extension` option.
- [3.6](#3.6) **Drop Method Prefixes When Accessing Properties from Java Getter Functions.**
When following the [JavaBeans naming conventions](https://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/) (as you should) to name your getter methods, you can access the properties with their property name directly. You should access properties this way for consistency and readability.
```html
${component.getTitle}
...
${component.title}
...
```**[⬆ back to top](#table-of-contents)**
- [4.1](#4.1) **Use the `sly` tag name for all elements that are not part of the markup.**
HTML elements with the tag name `sly` are automatically getting unwrapped and will not be part of the final markup.```html
``````html
``````html
...
...
```
**IMPORTANT** - The `sly` element will not automatically unwrap itself if you use HTL 1.0 (AEM 6.0). In that case, you still have to add the `data-sly-unwrap` attribute.
```html
```
- [4.2](#4.2) **Try to place `data-sly-use` statements only on top-level elements.**
Since `data-sly-use` identifiers are always global (https://docs.adobe.com/docs/en/htl/docs/use-api/java.html#Local%20identifier), these attributes should only be placed in the top-level element. That way one can easily see name clashes and also it prevents initializing the same object twice.
```html
${teaser.title}
${teaser.title}
```
- [4.3](#4.3) **Use meaningful identifier names.**
This will enhance the readability of your HTL scripts and and makes it easier for others to understand.```html
...
...
```
- [4.4](#4.4) **Use lowerCamelCase for identifier names.**Using lowerCamelCase (You start by making the first word lowercase. Then, you capitalize the first letter of each word that follows i.e.: "sampleIdentifierName") will help to increase the readability of your identifiers. Notice though that
HTL will internally only use (and log) full lowercase identifiers. Also dashes are not allowed for identifiers.```html
...
...
```- [4.5](#4.5) **Re-use expressions with identifiers**
If a test block statement is used multiple times, define an identifer and re-use it this way instead. This will allow the htl compiler to cache the expression result and will also make your code easier to read and understand.
```html
...
...
```Similarly, if a generic expression is used multiple times, define an identifer with `data-sly-set` and re-use it, for the same reasons stated above.
```html
```- [4.6](#4.6) **Use identifiers instead of the default “item” variable for list block statements.**
```html
```
- [4.7](#4.7) **Place block statements before the regular HTML attributes.**
The reason for that is that regular HTML attributes might use HTL variables which have been declared in the same element via `data-sly-use`. One should always declare things before using them. Also HTL block elements might influence if the element appears at all (via `data-sly-test`) or multiple times (via `data-sly-repeat`) and therefore are just too important to put them at the end of the attribute list. Further details in [issue 25](https://github.com/Netcentric/aem-htl-style-guide/issues/25).
```html
```
- [4.8](#4.8) **Use existing HTML elements for your block statements if possible.**
```html
…
…
```
- [4.9](#4.9) **Avoid the `element`, `attribute` and `text` block statements.**
It's a lot cleaner and explicit to write your HTL scripts without these block statements.
```html
${event.year}
${event.year}
```
- [4.10](#4.10) **Define your templates in a separate file.**
It's cleaner to create separate files for your template markup, so your HTL scripts will not get cluttered.
```html
${title}
${text}
${teaserModel.title}
${teaserModel.text}
```
- [4.11](#4.11) **Avoid using data-sly-test to set arbitrary variable bindings**
Instead of binding a variable with `data-sly-test`, use the purposefully defined `data-sly-set`. This avoids unintentionally hiding elements if the result of the expression evaluates to false (see [HTL expressions evaluating to false](https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#1151-boolean) ) and/or stopping the evaluation of further block statements; This is specially difficult to debug when various `data-sly-test` statements affect the same element.
```html
${fullName}
${fullName}
```
- [4.12](#4.12) **Avoid unnecessary `` tags.**
It's cleaner and easier to understand your intentions if you add your block statements in the relevant elements directly instead of wrapping them with an `sly` tag.
```html
${title}
${title}
```
- [4.13](#4.13) **Use an explicit `` end tag to close `` tags.**
Because `sly` is neither a void nor a foreign element (See [html5 start tags](https://html.spec.whatwg.org/multipage/syntax.html#start-tags)), it must be explicitly closed with an end tag ``. Using a self-closing tag is **not** allowed.
```html
```
**[⬆ back to top](#table-of-contents)**
## License
The MIT License (MIT)
Copyright (c) 2015 Netcentric
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**[⬆ back to top](#table-of-contents)**