In sightly I have initialized the sling model class. Now, I want to call the method from that class. But method name is read from some variable (basically I read method name from other location). When I use that variable it doesn’t work. I know sightly doesn’t allow expression inside an expression, so would like to know if there is an alternative to fit this need.
<sly data-sly-use.detailsModel="org.svc.core.model.DetailsModel"/>
${detailsModel.{methodNameVariable}}
- doesn’t work (if the method name is read from some variable)
${detailsModel.methodName}
- works (if put the method name directly there)
That really dependes on how you structured your data model and what you want to do with it.
If you want to do something like an if ... else
that can be achieved with:
<sly data-sly-test="${condition}">${model.someMethod}</sly>
<sly data-sly-test="${!condition}">${model.otherMethod}</sly>
If you want to do something like a switch ... case
:
<sly data-sly-test="${condition1}">${model.method1}</sly>
<sly data-sly-test="${condition2}">${model.method2}</sly>
...
If you want to have a trully dynamic method names then you’re better off with a model that returns a map of values instead of having a number of properties/methods and use the dynamic method name as a key:
${model.details[detailName]}