Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty array evals to false #107

Open
barisusakli opened this issue Feb 7, 2022 · 6 comments
Open

empty array evals to false #107

barisusakli opened this issue Feb 7, 2022 · 6 comments
Labels

Comments

@barisusakli
Copy link
Collaborator

I think should eval to true like javascript

{{{ if emptyArray }}}
doesnt hit there
{{{ end }}}
@barisusakli barisusakli added the bug label Feb 7, 2022
@julianlam
Copy link
Contributor

julianlam commented Feb 7, 2022

Would be a breaking change then, by the way.

Most usages currently use {{{ if emptyArray.length }}} which should work as expected even if this were changed, so that's fine.

@pitaj
Copy link
Member

pitaj commented Feb 7, 2022

This was explicitly chosen to be the case for backwards compatibility. The behavior is documented. Why specifically do you want it changed?

@barisusakli
Copy link
Collaborator Author

It is confusing because if (array) is always true in js but false in the templates. And array.length should be used to check for empty array. Yeah could be major version bump since its a breaking change.

@pitaj
Copy link
Member

pitaj commented Feb 7, 2022

Yes JS does it differently. But the templates are a different language (even if they do compile to JS). Do you have any use cases where this makes a difference?

@barisusakli
Copy link
Collaborator Author

I ran into it while working on browsing users https://github.com/barisusakli/nodebb-plugin-browsing-users/blob/master/index.js#L39 I just set it to true now and the template doesnt render anything with {{{ each browsingUsers }}}. Before that it was = [] but that evaluated to false.

@barisusakli
Copy link
Collaborator Author

barisusakli commented Feb 7, 2022

One exception is that an empty array is treated as a falsy value, so would not output anything.

This should be on the readme in bold if we are not fixing this 😆

@pitaj pitaj added question and removed bug labels Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants