简体   繁体   中英

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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