lilmessi42
lilmessi42

Reputation: 313

Use {{this}} as argument for helper in Meteor

I have the following helper function in Meteor:

sourceArray : function () {

        sources = [];

            for (var i = 0; i < images.length; i++) {
                if (!isInArray(images[i].source, sources)) {
                    sources.push(images[i].source);
                }
            }

        return sources; 
      }

I also have this helper function:

imageTitle : function (source) {
          var index = sources.indexOf(source);
          return images[index].title;
      }

In my HTML, I want to use the helpers like this, passing {{this}} as an argument:

<div class="row">
                {{#each sourceArray}}
                <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                    <a href="#" class="thumbnail">
                    <img class="img-responsive" src={{this}}>
                    </a>
                    <!-- THIS IS THE IMPORTANT BIT -->
                    {{imageTitle {{this}} }}

                </div>
                {{/each}}
        </div>

Upon doing so, I received the following error:

Errors prevented startup:

 While building the application:
 art.html:55: Expected identifier, number, string, boolean, or null
 ...                <h3>{{imageTitle {{this}} }}</h3>
 ...
 ^

What can I do to fix this?

Upvotes: 0

Views: 65

Answers (3)

CaptSaltyJack
CaptSaltyJack

Reputation: 16055

Any helpers used inside a helper block will automatically be able to reference this. See the below MeteorPad for an example:

http://meteorpad.com/pad/LMmMTSPi6BDtii4zb/ThisThing

Upvotes: 1

SirCharlesWatson
SirCharlesWatson

Reputation: 400

I'm on mobile right now but I'll give you a huge tip. Inside of the each block, you don't need to pass this to the helper. You can just call this in the JS and it'll refer to the same thing that you are currently trying to pass in.

Upvotes: 5

kaoskeya
kaoskeya

Reputation: 1091

You're looking for {{ imageTitle this }}

Upvotes: 3

Related Questions