How to set variable from use object in sightly?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to set variable from use object in sightly?



I have plain use class which contains one method that returns complex bean with a lot of setters/getters. Lets name it SomeUse. Given sightly file:


SomeUse


<sly data-sly-use.someUse="com.mycompany.SomeUse">
$someUse.data.firstProperty
<div>
$someUse.data.secondProperty
</div>
<!-- ...and so on -->
</sly>



So the point is I don't want to look at someUse.data getting. Instead of it I would do something like this:


someUse.data


<sly data-sly-use.someUse="com.mycompany.SomeUse" data-sly-use.data=$someUse.data>
$data.firstProperty
<div>
$data.secondProperty
</div>
<!-- ...and so on -->
</sly>



I can't do this way though. Is there any alternative to achieve such result? Thanks a lot!





why is java tagged ?
– StackFlowed
Dec 29 '15 at 15:09





Removed :) Thanks for pointing this out!
– Dawid Pura
Dec 29 '15 at 15:20




4 Answers
4



You can set variables for use in a WCMUse class with the following syntax:


<sly data-sly-use.someUse="$ 'com.mycompany.SomeUse' @ page=currentPage ">



and retrieve the page variable from your WCMUse class's activate() method like this:


page


activate()


Page page = get("page", Page.class);



For a working example, check out this Sightly script and this WCMUse class.





Documentation can be found here : docs.adobe.com/docs/en/aem/6-1/develop/sightly/… Parameters
– Bambara
Jan 4 '16 at 20:28



Instead of data-sly-use.data=$someUse.data
use data-sly-test.data="$someUse.data".
Then you will be able to get the property like


data-sly-use.data=$someUse.data


data-sly-test.data="$someUse.data"


$data.firstProperty
<div>
$data.secondProperty
</div>



So the answer is to:


<sly data-sly-test.varName="$data.firstProperty"></sly>
<div> $varName.secondProperty </div>



Creating a variable through empty data-sly-test attribute make it accessible after a tag.


data-sly-test



You can also set in DOM elements


<div id="elementId" data-sly-test.vehicle="$model.vehicle">
<p>$vehicle.name</p>
<p>$vehicle.type</p>
</div>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS