squirrellyjs / squirrelly

Semi-embedded JS template engine that supports helpers, filters, partials, and template inheritance. 4KB minzipped, written in TypeScript ⛺
https://squirrelly.js.org
MIT License
599 stars 82 forks source link

Cant create else if block #98

Closed sleekdevelopment closed 5 years ago

sleekdevelopment commented 5 years ago

I tried to implement else if block. is it possible ?

nebrelbug commented 5 years ago

@sleekdevelopment You would need to do something like this:

{{if(options.someval === "somevalue")}}
Display this!
{{#else}}
  {{if(options.someotherval === "someothervalue")}}
  Display this!
  {{#else}}
  They don't equal
  {{/if}}
{{/if}}

You could also define your own helper, say you called it IF:

Sqrl.defineNativeHelper('IF', {
    helperStart: function (param) {
      return 'if(' + param + '){'
    },
    helperEnd: function () {
      return '}'
    },
    blocks: {
      elseIfConditionA: function () {
        return '}else if (options.someOtherThing === true) {'
      }
    }
})
{{IF(options.someval === "someothervalue")}}
Display this!
{{#elseIfConditionA}}
They don't equal
{{/IF}}

Here's a CodePen demo I put together as well: https://codepen.io/nebrelbug/pen/NENaga Hope this clears things up!

nebrelbug commented 5 years ago

@sleekdevelopment I'm going to close this now. Hope you were able to find a good solution!