Add a new filter that replaces falsy values with a default value #162

wants to merge 1 commit into


None yet

4 participants

Include a filter that checks if a value if falsy and, in that case, replaces it with a default one (passed as an optional argument, empty string by default).

This is useful to make your intention clear (instead of <%= value || "my value" %>) and to combine it with other filters (e.g. <%=: value | default | capitalize %>).

It includes some tests.

@palmerabollo @palmerabollo palmerabollo Add default filter
Include a filter sets a default `def` value if `obj` is falsy.
If no default value is passed, it uses an empty string.

Includes unit tests
tj commented Mar 21, 2014

hmm this would maybe make more sense if it were a null == val check, but otherwise I think || is more clear, to me "default" would mean 0 and false would be allowed

You are right, it is not so clear and it does not add much value. Thanks for your comment.

yeasy commented Aug 14, 2015

an progress on this ?
If the given variable is undefined, then give a default value. This is quite useful.

@yeasy this won't be added to ejs. You can use <%= yourvariable || defaultvalue %> instead.

yeasy commented Aug 17, 2015

We can close this.

mde commented Aug 17, 2015

Just a note, this version of EJS is no longer supported. V2, which is the one on NPM, is here:

yeasy commented Aug 17, 2015

<%= yourvariable || defaultvalue %> seems not work.
I rend the ejs without giving the yourvariable value.
The error is yourvariable is not defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment