Understanding Sling Models in AEM

Understanding Sling Models in AEM

Learn about Sling Models from the Digital Experience Experts

Related Blogs:

New Features in AEM 6.1 Sightly

Sling Model with Sightly Part – I

Sling Models with Sightly Part – II ( Key Points )

Sling Models with Sightly Part – III (Key Annotations – I)

Sling Models with Sightly – Part IV (Key Annotations II)

Sling Models with Sightly Part – V (Key Annotations – III)

New Features in AEM6.1 Sightly

New Features in AEM 6.1 Sightly

In this blog, I will explain the new features in AEM6.1 Sightly. These features will make your sightly code shorter and simpler. These features are listed below.

  1. Introduction of <sly> tag
  2. data-sly-repeat
  3. URL manipulation
  4. local for i18n

I will explain these newly introduced sightly features in the form of questions and answers, so let’s start.

Q1) What is the use of <sly> tag?

This tag is used to remove extra divs generated when we include a resource in our component’s HTML file using Sightly code.

Q2) We can do that using data-sly-unwrap, why <sly>?

For explaining this, let’s consider we have to include a parsys component in our HTML without extra divs using data-sly-unwrap, then your code line will be

<div data-sly-resource="${@ path='par', resourceType='foundation/components/parsys'}" data-sly-unwrap="true" ></div> or
<div data-sly-resource="${@ path='par', resourceType='foundation/components/parsys'}" data-sly-unwrap ></div>

It will work fine but in this case, you have to first define a <div> tag to use data-sly-resource attribute and you also have to write data-sly-unwrap to remove this extra <div> tag.

Won’t it be simple if you didn’t have to write this extra <div> tag and data-sly-unwrap?
Q3) How to use <sly> tag? Or what is the syntax of <sly> tag?

If you want to write the same line using <sly> tag, then the code will be-

<sly data-sly-resource="${@ path='par', resourceType='foundation/components/parsys'}" /> or
<sly data-sly-resource="${@ path='par', resourceType='foundation/components/parsys'}"></sly>

Here, you can see the difference between old approach and <sly> tag.

Q4) Will <sly> tag not generate any extra divs?

Yes, it will not generate any extra <div> tag or any HTML DOM element, you can think of it as a replacement of data-sly-unwrap Sightly tag.

Q5) Is data-sly-unwrap deprecated or removed from AEM6.1 Sightly?

No, you can use it as well but as you have a much better <sly> tag I think data-sly-unwrap is not required. I think it is available only to support the AEM6.0 Sightly code.

Q6) What is the data-sly-repeat tag in Sightly?

This tag is used to iterate any of the list element.

Q7) Can we do that using data-sly-list in Sightly?

Yes, you can do that by using data-sly-list but the only difference is that, if you use data-sly-repeat, you will not have to provide any container element.

Q8) Not getting your point, could you please elaborate on your statement i.e. any container element?

Let us consider you want to iterate all child pages of currentPage object. Then, firstly use data-sly-list to write the code as

<div data-sly-list="${ currentPage.listChildren }">
      ${item.name}
</div>

Output will be generated as

test1 test2 test3 test4

And when you view the HTML element structure in your debugger it will be

<div>
   Test1
   Test2
   Test3
   Test4
<div>

Now use data-sly-repeat

<div data-sly-repeat="${ currentPage.listChildren }">
      ${item.name}
</div>

Output will be

test1
test2
test3
test4

And when you view the HTML element structure in your debugger it will be

<div>
      test1
</div>
<div>
      test2
</div>
<div>
      test3
</div>
<div>
      test4
</div>

You can see these generated DOM elements in the debugger’s Elements tab.

Q9) We can achieve same DOM structure using data-sly-list, is it required to use data-sly-repeat for the same?

No, it is not required but using data-sly-repeat your HTML code will become shorter. For e.g. If you want to achieve

<div>
      test1
</div>
<div>
      test2
</div>
<div>
      test3
</div>
<div>
      test4
</div>

output, then the code you have to write with data-sly-list will be

<div data-sly-list="${ currentPage.listChildren }" data-sly-unwrap>
       <div> ${item.name} </div>
</div>

Here you have to add data-sly-unwrap and <div> tag for each element. But by using data-sly-repeat, these tags will not be required and your HTML looks more refined and simple.

Q10) What is URL manipulation in AEM6.1 Sightly?

In AEM 6.1 you can do some URL manipulations, which are not available in AEM6.0 Sightly. Some of the examples are:
1. Adding extension with the code

${'content/geometrixx/en'@ extension='json'}

Output will be

content/geometrixx/en.json

2. Adding scheme (http or https) if required

 ${'content/geometrixx/en'@ scheme='http', extension='json'}

Output will be

http://content/geometrixx/en.json

3. Adding Selectors

${'content/geometrixx/en'@  selectors='s1', extension='html'}

Output will be

 content/geometrixx/en.s1.html

4. Adding Fragments

 ${currentPage.path @ selectors='s1.s2', extension='html', fragment='blog'}

Output will be

content/geometrixx/en.s1.s2.html#blog
Q11) What is the locale for i18n in the newly introduced sightly features?

This locale is a newly introduced object in AEM6.1. When we work with i18n in Sightly, we can use this object to set custom locale. It can be used as

${'Hello World' @ i18n, locale=request.locale}

Note –  This object is not present in AEM6

I am also sharing the Git repository link where you can find demo examples for these properties.
Git repository link is –

https://bitbucket.org/argildx/accunity-blog-snippets/src/master/

Happy coding!

Related Blogs:

Understanding Sling Models in AEM

Sling Model with Sightly Part – I

Sling Models with Sightly Part – II ( Key Points )

Sling Models with Sightly Part – III (Key Annotations – I)

Sling Models with Sightly – Part IV (Key Annotations II)

Sling Models with Sightly Part – V (Key Annotations – III)

 

ResourceResolver Object in AEM 6.1/6.0 Sling Services

ResourceResolver Object in AEM6.1,6.0 Sling Services

In this article, we’ll discuss eight related questions around ResourceResolver Object in AEM 6.0 or 6.1 Sling services that most developers working on this technology will also have.

As we know, getAdministrativeResourceResolver() method has been deprecated from ResourceResolverFactory interface in AEM6 and above versions. This brings up many questions in the minds of stakeholders, especially developers, involved in AEM projects. I’ve discussed some of those questions and provided answers to them.

Understand the use of Sling Models in AEM better through this video and the list of articles provided.

Q1) How to get ResourceResolver object in Sling services in AEM6.0 and above versions?

If you are working with AEM6 or AEM6.1, then you have two options to get ResourceResolver object.

  1. If you know the credentials of a user and want that user credential in your service, then go with getResourceResolver() method.
    Map<String, Object> param = new HashMap<String, Object>();            
    param.put(ResourceResolverFactory.USER,"admin");
    param.put(ResourceResolverFactory.PASSWORD,"admin".toCharArray());
    try {
          resourceResolver = resourceResolverFactory.getResourceResolver(param);
    }catch (Exception e){
          System.out.println(e.getMessage());
    }
  2. If you don’t have user credentials and want to access ResourceResolver object, then you have to use
    getServiceResourceResolver() method as shown below –

     

    Map<String, Object> param = new HashMap<String, Object>();
    param.put(ResourceResolverFactory.SUBSERVICE,"testService");
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);
    }catch (Exception e){
        System.out.println(e.getMessage());
    }

    Note : – In this case you have to add a configuration in the Felix Console of your AEM instance.

Q2) What configuration do I need to make for AEM6.0?

For AEM6.0, the configuration steps are –

  1. Go to Felix Console configuration tab i.e. http://<host>:<port>/system/console/configMgr
  2. Search for User Mapping as shown in figure-
  3. Click on this service to edit its configuration.
  4. Here you have to add one entry and the syntax of the entry is –
    “Bundle symbolic Name”:”SubServieName”=”User Name”

    1. “Bundle symbolic Name” :-  Here you have to add the bundle symbolic name where you are creating service.
    2. “SubServiceName”:- This is the name you provided as a value of  ResourceResolverFactory.SUBSERVICE property i.e. “testService”.
    3. “User Name” :- This is the user name for ex. “admin”So in my case this configuration field value becomes “com.blog.blog-bundles:testService=admin”
    After adding this entry, your configuration looks like
  5. Now everything is set for AEM6.0
Q3) Will these configurations work for AEM6.1?

No.

Q4) What extra configuration do I need for AEM6.1?

If you are working with AEM6.1, then you have to complete all the steps explained for AEM6.0. Then, to make these configurations working for AEM6.1, you have to ensure that the “jcr:PrimaryType” of your user is “rep:SystemUser” i.e if you are trying to use “admin” user. Then it will not work as its “jcr:PrimaryType” value is  “rep:User”

Q5) What do you mean by “jcr:PrimaryType” as “rep:SystemUser”?

It means that user is just not a repository user. It must be a system user.

Q6) How to create a System User in AEM6.0?

To create a System User, follow these steps-

  1. Go to CRX Explorer http://<host>:<port>/crx/explorer/index.jsp
  2. Login as Administrator i.e. using “admin:admin”
  3. Click on “User Administration”, you will see a screen just like this
  4. Here you will see the option of “Create System User.” Click on this button.
  5. Add a value for User Id field for ex. “testUser”
  6. Click on the green button.
  7. Close the window.
  8. Go to CRXDE Lite, your “testUser” will be created under /home/system directory. If you are not able to find it, then search for “testUser” in home screen. You will get the location.
Q7) How to use this user?

In your “Apache Sling Service User Mapper Service,” the configuration changes your entry from –
com.blog.blog-bundles:testService=admin to com.blog.blog-bundles:testService=testUser

Q8) Should I do this configuration at “Apache Sling Service User Mapper Service” configuration or should I create an “Apache Sling Service User Mapper Service Amendment” service configuration?

You can do it both ways. However, as “Apache Sling Service User Mapper Service” is a service factory in AEM6.1 so as best practices you should create another service configuration by clicking on the plus button in front of “Apache Sling Service User Mapper Service Amendment”.

When you click on the + button, one new configuration will be created at the end. Click on that service and do this configuration there. Your code will work in the same manner as before.

I hope I’ve been able to clear some doubts you had around ResourceResolver Object in AEM. If you’re still stuck with any other questions, shoot it out in the comments section.s

Happy coding!