Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Use a Meteor Template Helper to Edit a Value Passed as a Parameter in Iron-Router?

How do I use a template-helper to edit the value of the parameter I passed into a route created using the pathFor method of iron-router???

I have this template-helper:

Template.registerHelper('slugify', function(obj){
  return _.slugify(obj);
});

in my .html file I have this:

{{#each menuItemsFromDB}}
          {{#each arrayOfMenuItems}}
          <a class="item category" href="">
          {{this}}
          </a>
          {{/each}}
{{/each}}

The {{this}} in the above code returns a string, the name of the category. Because I have registered a template helper, I can SLUGIFY this category by:

{{slugify this}}

and then I have this route:

this.route('List',{
    path: '/list/:category_slug',
    template: 'list',
    controller: 'ListController'
  });

I can link to this route and pass a parameter to this route by:

{{pathFor 'List' category_slug='the-value-i-passed'}}

But that would be hard-coding it which cannot achieve the desired result I want.
I want it to be dynamic by using the 'slugify' template helper and iron-router's pathFor method and using the value of {{this}}.

What I'm trying to achieve is something like this although this code below doesn't work:

{{pathFor 'List' category_slug={{slugify this}} }}

What's the work around to achieve what I'm 'trying' to achieve with the above line????

I was hoping I can do something like:

{{pathFor 'List' category_slug=slugify(this) }}

or

{{pathFor 'List' category_slug='{{slugify this}}' }}
like image 952
preston Avatar asked Sep 28 '22 00:09

preston


1 Answers

Long story short, what you're looking for is not yet implemented using the current syntax, although it's part of the Handlebars standard implementation, upon which Meteor Spacebars is based.

For the moment, you have to create a separate helper that slugifies your input and call it within pathFor.

JS

Template.myTemplate.helpers({
  slugified: function(){
    return _.slugify(this);
  }
});

Spacebars

{{pathFor 'List' category_slug=slugified}}

Note that Handlebars sub-expression support is planned in a near future and might even make its way to the next version of Meteor according to this PR : https://github.com/meteor/meteor/pull/4101

like image 126
saimeunt Avatar answered Nov 15 '22 11:11

saimeunt