如何使用流星模板助手编辑铁路由器中作为参数传递的值?

时间:2022-11-04 19:41:35

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???

如何使用模板助手编辑传递给使用iron-router的路径方法创建的路由的参数值?

I have this template-helper:

我有这个模板助手:

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

in my .html file I have this:

在我的.html文件中我有这个:

{{#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:

上面代码中的{{this}}返回一个字符串,即类别的名称。因为我已经注册了模板助手,所以我可以通过以下方式来解决这个类别:

{{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}}.

但那将是难以编码的,它无法达到我想要的预期效果。我希望它是动态的,使用'slugify'模板助手和铁路由器的pathFor方法并使用{{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}}' }}

1 个解决方案

#1


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.

简而言之,您正在寻找的内容尚未使用当前语法实现,尽管它是Meleb Spacebars所基于的Handlebars标准实现的一部分。

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

目前,您必须创建一个单独的帮助程序,它将您的输入放大并在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

请注意,Handlebars子表达式支持计划在不久的将来,甚至可能根据此PR进入下一版Meteor:https://github.com/meteor/meteor/pull/4101

#1


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.

简而言之,您正在寻找的内容尚未使用当前语法实现,尽管它是Meleb Spacebars所基于的Handlebars标准实现的一部分。

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

目前,您必须创建一个单独的帮助程序,它将您的输入放大并在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

请注意,Handlebars子表达式支持计划在不久的将来,甚至可能根据此PR进入下一版Meteor:https://github.com/meteor/meteor/pull/4101