Expose js location hosted in AEM[6.3] clientlib as part of end user page : Bit urgent | Community
Skip to main content
cquser1
New Participant
August 6, 2017
Solved

Expose js location hosted in AEM[6.3] clientlib as part of end user page : Bit urgent

  • August 6, 2017
  • 4 replies
  • 2270 views

Hi All,

We have a requirement wherein

1] A js file is placed in etc/clientlib folder of AEM.

2] Now, this js location is to be available on end user facing site[ or say on publish environment ] along with some user instructions text and styling with it. Basically if these are available on publish/live site , the user will be able to copy all these [location of js file say localhost:4503/etc/clientlibs/.../../sample.js , instructions text and styling ]hosted on AEM servers and use it on some other website/page.

3] Based on the environment like dev, qa.. it should pickup the domain [for which we already have backend code to read from the configuration] and link to the js file should be appropriately formed.

Any thoughts/pointers/reference code on this will be really helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by susheel

Hi,
Below code works for me. It shows up in the viewsource.

<sly data-sly-test.domainvaluefromconfig ="${'http://www.sample.com'}"/>

${domainvaluefromconfig} <!--/*this prints as expected*/-->

<script type="text/javascript" src="${domainvaluefromconfig}/etc/designs/somedesign/js/script.js></script><!--/*this also has full url as expected*/-->

If you want to print it in a text area you need to do as below.

f you want to print it in a text area you need to as bw.

<textarea cols="30" rows="7">

&lt;script type="text/javascript" src="${domainvaluefromconfig}/etc/designs/somedesign/js/script.js/&gt;&lt;script&gt;

</textarea>

Try sightly escape check this forum link: AEM 6.1 | Escape special chars in Sightly

You can also use StringEscapeUtils from apache commons: StringEscapeUtils (Commons Lang 2.6 API)

You can pass the html tag to wcmusepojo and get the escaped value and pass it to the textarea.

<textarea cols="30" rows="7">

      ${escapedhtml}

</textarea>

4 replies

susheel
susheelAccepted solution
New Participant
August 7, 2017

Hi,
Below code works for me. It shows up in the viewsource.

<sly data-sly-test.domainvaluefromconfig ="${'http://www.sample.com'}"/>

${domainvaluefromconfig} <!--/*this prints as expected*/-->

<script type="text/javascript" src="${domainvaluefromconfig}/etc/designs/somedesign/js/script.js></script><!--/*this also has full url as expected*/-->

If you want to print it in a text area you need to do as below.

f you want to print it in a text area you need to as bw.

<textarea cols="30" rows="7">

&lt;script type="text/javascript" src="${domainvaluefromconfig}/etc/designs/somedesign/js/script.js/&gt;&lt;script&gt;

</textarea>

Try sightly escape check this forum link: AEM 6.1 | Escape special chars in Sightly

You can also use StringEscapeUtils from apache commons: StringEscapeUtils (Commons Lang 2.6 API)

You can pass the html tag to wcmusepojo and get the escaped value and pass it to the textarea.

<textarea cols="30" rows="7">

      ${escapedhtml}

</textarea>

cquser1
cquser1Author
New Participant
August 6, 2017

Hi All,

Am getting the domain value from backend .

<sly data-sly-use.config="com.project.core.models.ConfigModel">

${configModel.endpointDomain}

    </sly>

I get the correct value, say http://www.sample.com in my page.

If I try to use this data-sly-use variable in the line "<script type="text/javascript" src="${domainvaluefromconfig}/etc/designs/somedesign/js/script.js></script>", it doesn't work and nothing renders on the page.

Tried to use context uri and scriptString but didnt work.

feike_visser​ : Any thoughts on this will be helpful. Facing the above sightly related issue.

susheel
New Participant
August 6, 2017

Hi,

<script type="text/javascript" src="${domainvaluefromconfig}/etc/designs/somedesign/js/script.js></script>

Here domain value will change based on environment.

As you told that you have that config already you can read it in sightly and generate url dynamically.

Write a wcmusepojo or if you already have some code which reads config use it. Just concatenate and create the url.

cquser1
cquser1Author
New Participant
August 6, 2017

Just to add.

Even if this not part of any component in cq and if there is any other way we can show this on end-user page, that would also work.