Showing posts with label snippets. Show all posts
Showing posts with label snippets. Show all posts

Friday, 21 July 2017

Generic way to author text in AEM dialog

I am trying to author text in dialog, text contains dynamic values like name etc. I tried declaring sightly variable names in AEM dialog to fetch the data dynamically but it didn't work.

 I tried below approaches..

Approach 1:
  * Using String format
  String authoredText= "Hi %s, welcome to %s";
  authoredText = authoredText.format(authoredText,"Kishore","AEM Quickstart");"---Using String format----\n"+authoredText);

Approach 2:
  * Using StrSubstitutor - org.apache.commons.lang.text.StrSubstitutor
  Map valuesMap = new HashMap();
  valuesMap.put("name", "Kishore");
  valuesMap.put("blog", "AEM Quickstart");
  String templateString = "Hi ${name}, welcome to ${blog}.";
  StrSubstitutor sub = new StrSubstitutor(valuesMap);
  String resolvedString = sub.replace(templateString);"---Using StrSubstitutor----\n"+resolvedString);

Approach 3:
   * Using String replace
  String authoredText= "Hi ${name}, welcome to ${blog}";
  authoredText = authoredText.replace("${name}","Kishore");
  authoredText = authoredText.replace("${blog}","AEM Quickstart");"---Using String replace----\n"+authoredText);

From above approaches we can author the text in any of the format and parse it in java and we can get the text in HTL.

Sunday, 28 August 2016

AEM Code Snippets

// to get list of selectors in an array
String[] selectors = slingRequest.getRequestPathInfo().getSelectors();
// to get a page from a path
Page page = pageManager.getPage(path);
// to get any containing page for a resource
Page page = pageManager.getContainingPage(resourceResolver.getResource(path));
// to get a session in JSP
final SlingRepository repos = sling.getService(SlingRepository.class);
session = repos.loginAdministrative(null);

// to get query object and run a query
//build query using search in crx explorer or crxdelite
String stmt = "select * from cq:Page where jcr:path like '/content/training/%' and contains(*, '" + slingRequest.getParameter("q") + "') order by jcr:score desc";

Thursday, 11 August 2016

HTL (Sightly) Code Snippets

Below are few HTL syntax used frequently in AEM.

1. HTL does not support JSP tag libraries.

2. Including a script: data-sly-include instead of cq:include

<cq:include script="template.html"/> //Including a HTL file in JSP
<sly data-sly-include="template.jsp"/> //Including a JSP file in HTL

The element on which a data-sly-include has been set is ignored and not displayed.

3. Including a Resource: data-sly-resource

Includes the result of rendering the indicated resource through the sling resolution and rendering process. Basic syntax-

<article data-sly-resource="path/to/resource"></article>

Override resourceType of an include-
<article data-sly-resource="${'path/to/resource' @ resourceType='my/resource/type'}"></article>

Change the wcmmode-
<article data-sly-resource="${'path/to/resource' @ wcmmode='disabled'}"></article>