[SOLVED] Passing dynamic parameters to Spacebars includes

Does anyone know how to pass a dynamic parameter into a Spacebars helper?

The value of the parameter could be from the data context of the template/route that you’re in - or a javascript function that returns a string value, and the spacebars helper could be from a 3rd-party package that you don’t want to fork/alter.

I’ll give a very basic example:

<div>
  {{> sharejsText docid="this.document_id" id="editor" }}
</div>

So, this.document_id is the “dynamic” parameter brought in from the context, or it could be getDocumentId() from a JavaScript function, for example. But, we’re dealing with parsing it in the call for a Spacebars helper, so currently, that is being treated as a string literal. If you put it in braces like a helper, it doesn’t get parsed since we’re already inside of a helper call. Triple braces is just to make sure the content isn’t escaped, so that doesn’t work in this context either.

I’m sure this has been resolved prior, but I can’t seem to find any documentation or examples on how you would pass in a dynamic value to a Spacebars helper.

Does anyone have any tips or suggestions?

I have no trouble passing in dynamic data without braces and quotes, on the other hand I haven’t had to use helpers inside them before, so does this work?

 <div>
      {{> sharejsText docid=this.document_id id=editor}}
 </div>

Edit: I just tested it with a global helper and it works fine

1 Like

No, unfortunately, when I use the above, docid is never rendered in the final textarea output. I used this.active_document but that’s a String property (via SimpleSchema) that stores the project’s active document’s ID (confirmed via Robomongo and set through fixtures).

The app doesn’t crash, but it’s as if the data is never parsed out correctly.

You’re sure {{this.document_id}} returns correct data? Does this show the correct data in the div?

     <div>
          {{this.document_id}}
          {{> sharejsText docid=this.document_id id=editor}}
     </div>
1 Like

You’re right. I went back from the beginning and traced the “context” of the item throughout the route. It was lost prior to getting to that line of code.

I corrected it with a {{#with}} to correct the context, and it works as you stated.

Thanks!