Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

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

Avatar

Level 8

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.

1 Accepted Solution

Avatar

Correct answer by
Level 5

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>

View solution in original post

4 Replies

Avatar

Level 8

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.

Avatar

Level 5

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.

Avatar

Level 8

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.

Avatar

Correct answer by
Level 5

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>