This example shows how to build components in AEM 6 with Sightly. It's a feature-complete implementation of the famous TodoMVC exercise, which is traditionally meant for client-side JavaScript frameworks. This implementation though shows how such an application can easily be built in AEM and it's status persisted on the server using the Apache Sling REST framework.
All in about 300 lines of server- and client-side JavaScript code, following the latest AEM best practice!
Sightly is the new HTML templating system, introduced with AEM 6.0. It takes the place of JSP (Java Server Pages) and ESP (ECMAScript Server Pages) as the preferred HTML templating system for AEM.
The Sightly Documentation on the AEM Site is a great resource for getting started.
You can also read following blog posts:
- Sightly intro part 1
- Sightly intro part 2
- Sightly intro part 3
- Sightly intro part 4
- Sightly intro part 5: FAQ
- Sightly and Clientlibs
Get help from other Sightly users:
If you are interested to learn how we came up with Sightly, check out the Behind the Scenes Youtube channel.
The server sets a number of data attributes to the HTML elements that are interactive. These data attributes are instructing the app front-end about the asynchronous POST requests that are to be done when interacting with these elements, in order to persist the manipulations to the server. After each manipulation, the app content is reloaded through an asynchronous query to retreive the HTML fragment of the view that must be updated.
Beyond the classic MVC, this architecture also has following particularities:
- The Sling Post Servlet makes it possible that no code has to be written to handle the POST requests.
- Through the data attributes set by the server, the client needs no knowledge of how to structure the data for the Sling Post Servlet.
- To retreive the HTML fragment of what must be updated on the page, a simple
todoapp
selector allows to trigger the specific template that handles that part of the view. - The filters (to show all, or only active or completed items) use an additional selector, which allows the server to know which items to render.
Note that Java could also have been used instead of the server-side JavaScript files.
The nodes located in the content repository at /content/todo
are serialized in a .content.xml
file with following content:
jcr:content
The content node for the todo page. It contains the title and various labels that are to be displayed on the page, but important is thesling:resourceType
property, which makes this node rendered by thepage
component.
AEM components render individual content nodes based on their sling:resourceType
properties.
page
Renders the page content node.page.html
Entry point: The outer page markup that doesn't change when actions are performed.page.js
Server-side script that prepares additional data model needed bypage.html
.todoapp.html
Renders the list of todo items. Called directly when fetching what changed in the view.todoapp.js
Server-side script that prepares additional data model needed bytodoapp.html
; also defines the data attributes that will instruct the front-end POST requests.
item
Renders the todo item content nodes.utils
Collection of reusable scripts.filters.js
Defines what filters are set depending on the request selectors (i.e. to show all, or only active or completed items).
Client libraries can conveniently combine and minimize multiple CSS and JS files:
- Create package
- Get the code from this Git repository, for e.g. with following command line:
git clone https://github.com/Adobe-Marketing-Cloud/aem-sample-sightly-todomvc.git
- Create a ZIP file from the
app
folder, for e.g. in a Unix shell you can do:
cd aem-sample-sightly-todomvc/app
zip TodoMVC.zip -r jcr_root META-INF
- Get the code from this Git repository, for e.g. with following command line:
- Start AEM
- Double-click on the AEM JAR to start an instance if you haven't done that already.
- Once you're prompted with a login in your browser, enter
admin
as username and password.
- Install package
- Goto the AEM Package Manager.
- Click on
Upload package
and browse for your ZIP file, then hitOK
. - Once
TodoMVC.zip
shows up at the top of the list, click onInstall
=>Install
.
- Run the app
- Access the Todo page.