Sightly jstl c:set analog

夙愿已清 提交于 2019-12-14 02:44:58

问题


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

  1. Java class that extends the WCMUse class or implements the Use interface or
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!