Difference between cq:include and sling:include - aem

What is the difference between sling and cq includes and when should each one be used? I kinda find both solving the same purpose.
There is this link which recommends use of sling include and then there is cq doc which recommend use of cq include. As a programmer, under which situation will each of these includes be apt?

Usually if you are developing a CQ component, I'd follow the suggestion to use the cq:include. The sling:include has some additional parameter which can come handy if you need them, but I think in my 5 years as a CQ dev, I only had to use the sling:include once.
See sling documentation for the parameter: http://sling.apache.org/documentation/bundles/sling-scripting-jsp-taglib.html#include

If you have a component that has more than one jsp,to invoke the other jsp which doesnt match up with component name , sling include is helpful having replaceselector attribute having other jsp as a value

Related

Sightly syntax to retrieve crx/de properties in jsp/html page

I would like to retrieve some properties e.g. jcr:created using Sightly or any related syntax for the panel component in AEM adaptive forms. 1
The previous Sighlty syntaxes that I have attempted to retrieve the crx/de properties include:
${properties.jcr:created}
${pageProperties\[jcr:created\].getTime.toString}
${guidePanel.jcr:created}
${resource.jcr:created}]
I have tried the following syntaxes but unable to retrieve the value from the property and in worst cases, the component may not be rendered on screen.
I have looked up on Adobe forum sites and past stackoverflow questions that other people may have asked. I have tried the solutions and given answers but was unable to achieve the result. I would greatly appreciate for any help or sharing of applicable knowledge if you have encountered similar issues or previously attempted to solve similar problems. Thank you!
You can always create Use class in Java or Javascript to access these properties, it's cleaner and testable. Better than that, you can use Sling Models which are more readable and easier to implement, here is a good presentation about it https://www.slideshare.net/accunitysoft/understanding-sling-models-in-aem
Properties of resources are readable without any extra definitions needed.
So you might want to check what resource is handled in your component by adding this:
${resource.path}
Then you can check the returned path in CRXDE if there really is a jcr:created property available at this path - which should be the case if you are accessing a resource that has a proper sling resource type. Then this call should give a valid return value:
${resource.path} ___ created:
${properties.jcr:created.getTime.toString}
If the path is not displayed as well, then it would help if you could post
the repo path of the content resource you are processing
the sling:resourceType
the path to the component's ht(m)l file that you are using

Implementing custom Options in Adobe AEM HTL (formerly known as Sightly)?

This question is about Options, a feature in Adobe's proprietary language HTL:
https://helpx.adobe.com/experience-manager/htl/using/expression-language.html
Expression Option Sightly
My simple question: Can I extend HTL by implementing my own custom Options? If so, how?
Yes, you can implement your own custom options for an HTL expression. You will need to implement a Filter and add it to the compiler.
But you shouldn't, as:
This is not a designed extension point. You will need to fork the implementation and modify it, thus becoming responsible for keeping it up-to-date further down the road.
You can most probably get the same results by using the public APIs (such as https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/master/SPECIFICATION.md#221-use).
In case you think there's a very good reason for adding or modifying expression options, you can propose an HTL specification change (and also contribute the implementation in Sling).
No. To implement custom options you will have to extend the respective plugin that supports that expression. those classes are not exported by sightly scripting compiler bundle and are not available to be customized.

AEM/Sling: How do I implement dynamic sling selector?

TLDR: I want setup an AEM page that accepts firstname and lastname as parameter using an SEO friendly URL.
Going to www.host.com/mycontent.richard.williams.html will display information relevant to Richard Williams.
If I go to www.host.com/mycontent.john.smith.html, the page will display information relevant to John Smith.
SEO friendly example: www.host.com/mycontent.richard.williams.html
not SEO friendly example: www.host.com/mycontent.html?firstname=richard&lastname=williams
So I've been following this guide (not sure if this is the best example/guide to help me): http://www.aemcq5tutorials.com/tutorials/sling-servlet-in-aem/
And while it works well for my the example
#SlingServlet(resourceTypes="geometrixx/components/hompepage", selectors={"firstname","lastname"}, extensions="html",methods="GET", metatype=true)
I am trying to make it dynamic.
At the moment, I can only access the servlet if access via: http://localhost:4502/content/geometrixx/en.firstname.lastname.html
I want to make firstname and lastname dynamic parameters/selectors. Like if for example I want to pass the values "richard.williams", I can then use: http://localhost:4502/content/geometrixx/en.richard.williams.html
if I try to use http://localhost:4502/content/geometrixx/en.richard.williams.html right now, I get a blank page.
So basically I want to use selectors for passing parameter values to my page.
Any ideas how this can be done?
ps. At the moment, I only testing/experimenting in my local instance of AEM.
Selectors in sling don't provide the functionality of placing variables in the URL path. i.e. you cannot add {pathParam} like in Spring to sling servlet URL.
In general, selectors are not recommended to be used like an input to a function. They are to be used more like file extensions.
For eg. A request to /mycontent.html returns the same resource as /mycontent.mobile.html. The only difference being, the latter requests for a mobile friendly version.
Request params on the other hand serve the purpose of providing inputs to the servlet.
I cannot think of any direct way to attach a servlet to dynamic paths in sling. You can try using suffix, they are cacheable in the dispatcher, but I can't comment on the SEO friendliness of using suffix.
Consider this URI - /mycontent/user.json/john/smith
Register a servlet using the path /mycontent/user and you can use
String[] names = request.getRequestPathInfo().getSuffix().split(suffix, '/')
to retrieve the suffix contents.
Take a look at answers in this question. Sling ResourceProvider and integrating with jax-rs are other ways you can accomplish this.
Technically this would be feasible with the use of OptingServlet (see https://sling.apache.org/documentation/the-sling-engine/servlets.html#optingservlet-interface)
Your accepts method could easily recognise the expected resourceType and react appropriately.
As mentioned in the docs this approach is discouraged and the solution proposed by Subhash to use suffixes seems way more elegant.
You can create a components which reads selector from the request and add that compoent to en.html

cq:includeClientLib in AEM if included inside a component jsp and the component is present twice on the page

If we give cq:includeClientLib inside my component jsp and if we drag and drop the component twice on that page, will the clientlib gets loaded/included twice?
what will be the case if we do in Sightly way (data-sly-call="${clientlib.all # categories='somecategory'}") ?
And also what is the suggested method of including client libs, either create a clientlib specific to the component and load only for that component or include all the CSS and JS at a common clientlib and use it across?
No, the clientlib is only included once for a category.
This is by design as the HTL (and respective JSP tag) are evaluated during runtime and the processor keeps a map of categories that have already been included and does not include them again.
As #i.net mentioned, each category will only be included once. To answer your follow up question about the suggested method..
The best practice seems to be to define a client library for each component, which is then embedded into a "global" client library. That global client library will then be included within your page template.
/etc/designs/acme/clientlibs-all
categories=["acme-all"]
embed=[compA,compB]
/apps/acme/components/compA/clientlibs
categories=["compA"]
/apps/acme/components/compB/clientlibs
categories=["compB"]
The reason the global client library is located under /etc/designs is to prevent exposing /apps to the public. However, in AEM 6.3, you could make use of the allowProxy property to serve the code at /etc.designs/. This would then look like this:
/apps/acme/clientlibs/clientlibs-all
categories=["acme-all"]
embed=[compA,compB]
allowProxy=true
/apps/acme/components/compA/clientlibs
categories=["compA"]
/apps/acme/components/compB/clientlibs
categories=["compB"]
Adobe recently released a good tutorial of more recent best practices around client library structure: https://helpx.adobe.com/experience-manager/kt/sites/using/getting-started-wknd-tutorial-develop/part3.html

where does "request" come from in CQ5?

I am new to CQ5.
While I was trying to follow the official guide of how to create a website at
[https://docs.adobe.com/docs/en/cq/5-6-1/howto/website.html], the following code confused me.
Iterator<Page> children = rootPage.listChildren(new PageFilter(request));
I did not find any reference of "request". Can someone tell me where it comes from?
Thanks in advance.
Well request is an instance of the HttpServletRequest that is available implicitly in any JSP file. (Full list here).
Hence you can use them directly without explicitly declaring them.
In AEM (CQ5), apart from the JSP implicit objects, you can get quick access to few AEM, Sling and JCR specific objects by including the global.jsp file in your JSP as shown below.
<%#include file="/libs/foundation/global.jsp"%>
This entire list of objects and tag libraries that would be available through your global.jsp can be found here.