Thursday, October 13, 2016

Reading json file from i18 folder  and return from server side javascript and use as "use api" in sightly :

"use strict";
use([], function () {
  var resourceResolver = resource.getResourceResolver();
  var resourceContent = resourceResolver.getResource(file path in project + ".json/jcr:content");
  var jsonText = "";
  if (resourceContent != null) {
    var WCMUtils = Packages.com.day.cq.wcm.commons.WCMUtils;
    var stringWriter = java.io.StringWriter;
    var rd = new java.io.BufferedReader(new java.io.InputStreamReader(resourceContent.properties["jcr:data"], java.nio.charset.Charset.forName("UTF-8")));
    var sb = new java.lang.StringBuilder();
    var cp;
    while ((cp = rd.read()) != -1) {
      sb.append(java.lang.Character(cp));
    }
    jsonText = sb.toString();
  }
  if (jsonText == "") {
    jsonText = "{}";
  }
return {
  items : JSON.parse(jsonText);
  json : jsonText;
};
});

2 comments:

  1. What does "items" return? A list of key/value pairs? Trying to figure out the HTL/Sightly part of this function.

    ReplyDelete
  2. It will return json from i18n file as it is , which you can use in sightly using use api as in https://docs.adobe.com/docs/en/htl/docs/use-api/javascript.html

    ReplyDelete

Powered by Blogger.

Followers

Best Price Amazon Portal

Amazon Best Offers