Last active
September 13, 2015 00:28
-
-
Save auniverseaway/0fe4b7148bd92663d740 to your computer and use it in GitHub Desktop.
Detect String Array in Sightly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template data-sly-template.view> | |
<sly data-sly-test="${properties.options[1]}" data-sly-list.child="${properties.options}" data-sly-unwrap> | |
<label><input type="checkbox" name="${properties.name}" value="${child}">${child}</label> | |
</sly> | |
<label data-sly-test="${!properties.options[1]}"><input type="checkbox" name="${properties.name}" value="${properties.options}">${properties.options}</label> | |
</template> |
Now if you have Sightly 1.0 (and not 1.1), then what you do is very clever, but I'd rather use a very simple Use-API to detect if it is an array or not, and keep the template as straightforward as possible:
<sly data-sly-template.view data-sly-use.logic="${'logic.js' @ options=properties.options}" data-sly-list="${logic.options}">
<label><input type="checkbox" name="${properties.name}" value="${item}"/> ${item}</label>
</sly>
logic.js:
use(function () {
var options = this.options;
return {
options: Array.isArray(options) ? options : [options]
}
});
Just a heads up that Array.isArray was returning my String[] in JCR as a comma separated string. I had to change it a little...
logic.js:
use(function () {
var options = this.options;
return {
options : options.constructor === Array ? options : [options]
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Sigthly 1.1,
data-sly-list
anddata-sly-repeat
also iterate (once) over non-iterable elements:will output:
So in your case, you should be able to simply write your template as follows: