问题
On jsp I can write following code:
<c:set var="salary" value="${object.salary}"/>
and then in code I can use ${salary}
variable
Can you help to rewrite it using sightly?
P.S.
I tried this:
<div data-sly-use.salary="MySalary">
<div data-sly-use.product="MyBean" data-sly-unwrap>
{
<ul data-sly-list="${product.specifications}" data-sly-unwrap>
"${item.sku ? item.sku : 'product'}" : "${item.label} ${item.value}"<div data-sly-test="${!itemList.last}" data-sly-unwrap>,</div>
</ul>
}
</div>
</div>
<div data-bla-bla="${salary}"></div>
But I see compilation error
回答1:
You don't have the feature of assigning variables in Sightly. The maximum that is possible is comparing values, else all your logic goes into the Java or JavaScript Use-API
classes.
When you are using data-sly-use
, the value provided must either be
- Java class that extends the
WCMUse
class or implements theUse
interface or - JavaScript file that defines the use class.
Hence you receive a compilation error when the value of data-sly-use
is neither of the above two.
However, I can suggest a workaround for your question, though I wouldn't recommend using the same.
You can make use of data-sly-test
and assign a variable name to that, which can later be used elsewhere.
For example.
<div data-sly-test.salary="MySalary"></div>
<div>
<div data-sly-use.product="MyBean" data-sly-unwrap>
{
<ul data-sly-list="${product.specifications}" data-sly-unwrap>
"${item.sku ? item.sku : 'product'}" : "${item.label} ${item.value}"<div data-sly-test="${!itemList.last}" data-sly-unwrap>,</div>
</ul>
}
</div>
</div>
<div data-bla-bla="${salary}"></div>
来源:https://stackoverflow.com/questions/32247225/sightly-jstl-cset-analog