Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Hi! I have been asked in an interview that why sling model is needed when we can use Sightly to get property values directly?

Avatar

Level 1

Could someone please help me in answering this question in most understanding way possible?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @vijayku8 ,

Here is my take on Sling Models.

 

For simple things like printing properties of a page, Sightly can surely be used and having a Sling Model in such situations will be redundant.

Sightly is a templating language and its primary purpose being "to display data". It does have some capability to calculate values and loop over lists, but as for any templating language, those capabilities are limited.

 

- In situations where additional conditions, code statements fetching data from another page/service is needed, using a Sling Model is a good choice.

- When we need Logic to format email addresses or format/validate date inputs or validate if the the file browsed is an asset or a page.

- Fetch data from a service/servlet to show rating/reviews, count of pages, breadcrumb, build navigation, and similar dynamic data related functionalities.

 

Some of the simple checks can be done in using Sightly frontend however, that would make the Sightly code difficult to debug and maintain at later stages of the project.

 

Using Sling Models, the code is kept clean and maintainable.

 

thanks,

Preetpal

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hello @vijayku8 ,

Here is my take on Sling Models.

 

For simple things like printing properties of a page, Sightly can surely be used and having a Sling Model in such situations will be redundant.

Sightly is a templating language and its primary purpose being "to display data". It does have some capability to calculate values and loop over lists, but as for any templating language, those capabilities are limited.

 

- In situations where additional conditions, code statements fetching data from another page/service is needed, using a Sling Model is a good choice.

- When we need Logic to format email addresses or format/validate date inputs or validate if the the file browsed is an asset or a page.

- Fetch data from a service/servlet to show rating/reviews, count of pages, breadcrumb, build navigation, and similar dynamic data related functionalities.

 

Some of the simple checks can be done in using Sightly frontend however, that would make the Sightly code difficult to debug and maintain at later stages of the project.

 

Using Sling Models, the code is kept clean and maintainable.

 

thanks,

Preetpal

Avatar

Community Advisor

Hi @vijayku8 

 

Look, Sightly's great for grabbing simple stuff, like titles or images. But when things get tricky - complex logic, formatting, caching - Sling Models become your best bud. Think of them as data wranglers, transforming raw JCR into shiny, ready-to-use content for Sightly.

They save you time by:

  • Keeping Sightly tidy: No more messy logic cluttering your templates. Models handle the dirty work behind the scenes.
  • Boosting performance: Caching frequently used data means faster page loads, happy users, and less stress on your server.
  • Making you a testing hero: Inject mock data directly into models, so you can test components without needing AEM.

Plus, models handle security like a boss, ensuring only authorized eyes see sensitive info. Sure, you can grab a burger without a spatula, but it'll be messy. Sling Models are your spatula, making AEM development smoother, faster, and more secure. Trust me, they're not just hype, they're developer magic

 

Thanks

Avatar

Community Advisor

This article, written by one of Adobe's champions, provides one of the best insights on AEM Sling Models. You can read it here: https://www.bounteous.com/insights/2020/07/22/aem-sling-models-why-bother.


Quote: "Some common perceptions are that sling models for these components represent unnecessary complexity (arguable in some aspects, but not true in the big picture) and that development is simpler and quicker, especially for front-end developers, by omitting sling models (also not true).

Sling models are recommended for all AEM components, complex or simple, and building them via standard practices saves development time in both initial implementation and ongoing maintenance."

 

Just to add my two cents here. This is a best practice because it maintains a good design pattern for the overall architecture, encompassing both Sling and your code.


Hope this helps



Esteban Bustamante

Avatar

Community Advisor

As mentioned by others simple tasks like printing properties of a page or component without any logic, Sightly can be used without creating any sling model.

However, there are many benefits of using Sling Models in conjunction with Sightly.

1. Sling Models provide a structured approach to building components. With Java annotations, you can easily map properties from your JCR repository to your model class, making it more maintainable and reusable.Sighty, on the other hand, allows for cleaner and more readable HTML templates. With its simplified syntax and separation of concerns between markup and logic, it becomes easier to collaborate with front-end developers who may not have extensive knowledge of Java.

2. Sling Models with Sightly enables better testability. You can write unit tests for your models independently of the view layer, ensuring data integrity and logic correctness.

 

Additionally, when using Sling Models and Sightly together, you can take advantage of caching mechanisms provided by AEM. The built-in caching capabilities help improve performance by reducing unnecessary server-side rendering.

Thanks

Dipti