{{else}}
{{# helper() }} TRUTHY {{ else }} FALSY {{/ helper }}
Creates an FALSY
block for a helper function’s
options argument’s inverse
property.
The following creates a isSquare
helper that renders the FALSY
section
if the number is not a perfect square:
import {stache} from "can";
const view = stache(`<div>{{# isSquare(3) }}YES{{else}}NO{{/ isSquare}}</div>`);
stache.addHelper("isSquare", function(num, options){
if( Number.isInteger( Math.sqrt(num) ) ) {
return options.fn();
} else {
return options.inverse();
}
});
var fragment = view();
console.log(fragment.firstChild.innerHTML) //-> NO
document.body.appendChild(fragment);
Parameters
- FALSY
{sectionRenderer(context, helpers)}
:A partial stache template converted into a function and set as the helper function’s options argument’s
inverse
property.
Use
For more information on how {{else}}
is used checkout:
- if - renders the
FALSY
section if the expression evaluates to a falsy value.import {stache} from "can"; const view = stache(`<div> {{# if(this.value) }} TRUTH {{ else }} FICTION {{/ if }} </div> `); var fragment = view({value: 0}); console.log(fragment.firstChild.innerHTML) //-> "FICTION" document.body.appendChild(fragment);
const view = stache(<ul> {{# for(value of this.values) }} <li>{{ value }}</li> {{ else }} <li>no values</li> {{/ for }} </ul>
);
var fragment = view({values: []}); console.log(fragment.firstChild.innerHTML) //-> "
document.body.appendChild(fragment);
<div class='codepen' data-codepen=''></div>