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
571 stars 81 forks source link

Missing null filter? #209

Closed Indribell closed 3 years ago

Indribell commented 3 years ago

When data consists of the following:

{"ID":18,"firstname":"Jef","lastname":"","age":null}

    <ul class='list'>
    {{@foreach(it.data) => key, val}}
        <li class='item'>{{key}} - {{val.firstname}} - {{val.age}}</li>
    {{/foreach}}
    </ul>

The result will be displayed as:

20 - Dirk - 14
21 - Jef - null

Is there no filter to automatically remove "null" from being displayed?

nebrelbug commented 3 years ago

@Indribell good question! The best way is probably to define a default filter:

Sqrl.filters.define("default", function (val) {
  if (val === null) {
    return ""
  } else {
    return val
  }
})

Sqrl.defaultConfig.defaultFilter = "default"

Let me know if this solves your problem!

Indribell commented 3 years ago

@nebrelbug

This works perfectly. It may be interesting to add this to the documentation for other people to find?