Dawid Pura
Dawid Pura

Reputation: 1029

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:

<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:

<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!

Upvotes: 2

Views: 10367

Answers (4)

Md Rahman
Md Rahman

Reputation: 1820

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>

Upvotes: 0

Dawid Pura
Dawid Pura

Reputation: 1029

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.

Upvotes: 3

Nidhi Saxena
Nidhi Saxena

Reputation: 11

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.firstProperty}
  <div>
    ${data.secondProperty}
  </div>

Upvotes: 1

Bruce Lefebvre
Bruce Lefebvre

Reputation: 411

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 page = get("page", Page.class);

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

Upvotes: 1

Related Questions