简体   繁体   English

Handlebars.js:是否可以将多个散列作为参数传递给Handlebars助手?

[英]Handlebars.js: Is it possible to pass multiple hashes as arguments to a Handlebars helper?

Let's say I have declared following helper 假设我已经声明跟随助手

Handlebars.registerHelper("linkTo", function(request, params) {
    return window.linkTo(request, params);
});

how can I (if possible) do something like this in the view (not correct syntax, just for show purposes) 我如何(如果可能)在视图中执行类似的操作(语法不正确,仅用于显示目的)

<a href="{{link_to module='products' controller='view', product=product.id artist= artist.id}}">foo</a>

the comma just symbolises separation between the two hashes. 逗号只是表示两个散列之间的分隔。

,由于Handlebars.js使用参数的方式,因此无法分隔多个哈希。

If you can see your way to putting your parameters into a structure that you can pass as part of an object literal then I know that will work. 如果您可以看到将参数放入结构中的方法,并且可以将其作为对象文字的一部分进行传递,那么我知道这是可行的。 For example: 例如:

{ 
  "params" : {
    "module" : "products",
    "controller" : "view",
    "product" : 5,
    "artist" : 25
  }
}

Plus this helper: 加上此助手:

Handlebars.registerHelper("link_to", function(params) {
  var result = "";
  var insertAmpersand = false;

  for (var prop in params) {
      if (insertAmpersand) {
          result += "&";
      } else {
          insertAmpersand = true;
      }

      result += prop + "=" + params[prop];
  }
  return result;
});

Can be called like so: 可以这样称呼:

{{link_to params}}

To generate: 产生:

module=products&controller=view&product=5&artist=25

Obviously you would want to refine that so that things had the right quote marks around it, etc. But would something like that work for you? 显然,您希望对其进行优化,以使事物周围带有正确的引号,等等。但是这样的事情对您有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM