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

Trying to get comparisons working in an #each loop #247

Open
cfjedimaster opened this issue Mar 3, 2017 · 20 comments
Open

Trying to get comparisons working in an #each loop #247

cfjedimaster opened this issue Mar 3, 2017 · 20 comments

Comments

@cfjedimaster
Copy link
Contributor

For the life of me, I cannot get comparison, or if, to work inside an each loop. I'm currently trying this:

<select name="creator" id="creator" required>
	<option value=""></option>
	{{#each users as |user|}}
	<option value="{{user._id}}"
	{{#is @root.project.creator._id ../user._id}}
	selected
	{{else}}
	1 {{@root.project.creator._id}} 2 {{user._id}}
	{{/is}}
	>{{this.name}} </option>
	{{/each}}
</select>

In my comparison, on the right hand side, I tried @root.user._id, user._id, and what you see above. Nothing seems to work.

By the way, I know the {{else}} branch is creating invalid HTML, but I'm just using it to try to debug.

@jonschlinkert
Copy link
Member

Hmm, I would have thought that user._id would work, as you mentioned. Have you tried using {{log .}} inside or outside the loop to see what's on the context?

@cfjedimaster
Copy link
Contributor Author

I added it in my else block and it outputs nothing.

@jonschlinkert
Copy link
Member

If it works outside of the block, that indicates that each isn't iterating over the users variable. that isn't getting populated somehow

@cfjedimaster
Copy link
Contributor Author

Oh sorry if it wasn't clear. :) The each works perfectly. The drop down shows every existing user. It is the comparison that is failing, it never matches and outputs selected where it should.

@jonschlinkert
Copy link
Member

jonschlinkert commented Mar 3, 2017

Thanks for the clarification. Hmm.. with templates it's hard to debug without having the code in front of me, but what usually helps for me is to log out every variable, in every part of the loop, starting at the top.

First, maybe try creating a custom log helper too (fwiw I've been meaning to push up changes to the existing log helper, because it logs out the handlebars options, which makes it hard to see what's happening).

Something like:

function log() {
  var args = [].slice.call(arguments);
  args.pop() // get rid of hbs options
  console.log.apply(console, args);
}

Then just log out all of the variables in each step and see where the comparison is breaking down:

{{log "user" user}}
{{log "user._id" user._id}}
{{log "../user._id" ../user._id}}
{{log "@root.project.creator._id" @root.project.creator._id}}
<!-- here, I think "this" would be "user" since you're inside the each block, but I'm not sure -->
{{log "this.name" this.name}} 

It might be that the is helper is failing for some reason, maybe the comparison is too strict?

@cfjedimaster
Copy link
Contributor Author

Would "is" be using === maybe? As it stands, I first tried comparison with ==, it didn't work either.

@jonschlinkert
Copy link
Member

Here is the code for is. It is doing ===

@cfjedimaster
Copy link
Contributor Author

Well in theory - both are Mongo ID values, but - I don't know. I'll keep logging and let you know (may be delayed - giving a presentation in a bit and wifi is horrible here)

@jonschlinkert
Copy link
Member

Hope this isn't holding up your presentation, anything I can do to help?

@cfjedimaster
Copy link
Contributor Author

Oh no - heh sorry - totally different worlds. (preso on Apache Cordova, this work is a side project)

@jonschlinkert
Copy link
Member

good! yeah let me know when/if you have more detail, happy to help.

@cfjedimaster
Copy link
Contributor Author

So given I'm adding all the helpers from the lib directly into my Handlebars instance, how do I append the custom log function above? Currently I've got:

var exphbs = require('express-handlebars');
var helpers = require('handlebars-helpers')();
... 
app.engine('handlebars', exphbs({
defaultLayout:'main',
helpers:helpers
}));
app.set('view engine', 'handlebars');

@cfjedimaster
Copy link
Contributor Author

nm - just added it to hlpers before passing it to handlebars

@cfjedimaster
Copy link
Contributor Author

The log helper helped a lot. This is now working:

			{{#each users as |user|}}
			<option value="{{user._id}}"
			{{#is @root.project.creator._id ._id}}
			selected
			{{else}}
			  else  {{log .}}
			{{/is}}

Specifically: ._id. I don't quite get that. I know "dot" represents the current user in the each, but the as part should make user work too.

@jonschlinkert
Copy link
Member

Great! I wonder why user isn't working as expected, though. Oh, if you're curious, try @user, see if that works.

@cfjedimaster
Copy link
Contributor Author

Nope, doesn't help. I assume you meant @user._id.

@jonschlinkert
Copy link
Member

Either. It's odd that @user._id doesn't work, but ._id does... Since this should be @user (at least that's my understanding). In any case, sounds like you got it resolved?

@cfjedimaster
Copy link
Contributor Author

Yep. Maybe this is something that could be added to the docs? Or to a FAQ (don't know if one exists). Otherwise, just close it and thank you again!

@jonschlinkert
Copy link
Member

@cfjedimaster I'd be happy to, I'm not sure this was something related to this lib. I'm still confused as to why this !== user. It seems like the context is wrong there for some reason, since doing as |user| makes user available as @user and this inside the block scope.

@noroxi7
Copy link

noroxi7 commented Aug 9, 2021

Someone get the answer of this? because i have the same problem!!
I saw that compare works inside de each, but the problem is it with two variables, when i do it width a string and variable works, but width two variables it doesn't !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants