简体   繁体   English

将 Handlebars 组件输出作为参数传递给助手

[英]Passing a Handlebars component output as a parameter to a helper

I'd like to use the output of a component as a parameter to my helper, but am not having much success.我想使用组件的输出作为我的助手的参数,但没有取得太大的成功。

My Ember template is something like this:我的 Ember 模板是这样的:

<td class="wrap">
  {{generate-url 'http://stackoverflow.com' 'Visit'}}
</td>

and the helper:和帮手:

import { helper } from '@ember/component/helper';
import { htmlSafe } from '@ember/string';

export function helperFunction (params) {
    if (!params) {
        return "";
    }

    var link = params[0];
    if (!link) {
        return  "";
    }

    var linkText = params[1];
    if (!linkText) {
        linkText = link;
    }

    return new htmlSafe('<a href="' + link + '">' + linkText + '</a>');
}

export default helper(helperFunction);

The two combined successfully generates:两者结合成功生成:

<td class="wrap">
  <a href="http://stackoverflow.com">Visit</a>
</td>

Now I have a component (my-component.hbs) that outputs HTML with the following:现在我有一个组件 (my-component.hbs) 输出 HTML 如下:

{{#if name}}
  <span class="red">{{longName}}</span>
{{/if}}

This component works if it is referenced in the template like so: {{my-component name="abc" longName="alphabet"}} (renders <span class="red">alphabet</span> as expected).如果在模板中像这样引用此组件,则该组件有效: {{my-component name="abc" longName="alphabet"}}<span class="red">alphabet</span>预期呈现<span class="red">alphabet</span> )。

But I am stumped trying to get the HTML output into the second parameter of generate-url to produce:但是我很难将 HTML 输出放入generate-url的第二个参数中以生成:

<td class="wrap">
  <a href="http://stackoverflow.com"><span class="red">alphabet</span></a>
</td>

I have tried naively {{generate-url 'http://stackoverflow.com' (my-component name="abc" longName="alphabet")}} , but that outputs errors when that particular Handlebar is evaluated.我曾天真地尝试过{{generate-url 'http://stackoverflow.com' (my-component name="abc" longName="alphabet")}} ,但是在评估该特定 Handlebar 时会输出错误。

Thanks for the clarification it was a bit hard to understand at first.感谢您的澄清,起初有点难以理解。

I don't use ember.js but just the raw Handlebars.js.我不使用 ember.js,而是使用原始的 Handlebars.js。 However to my mind you can't chain helpers to pass results from one to another, even the builtin lookup helper will not help.但是,在我看来,您不能链接助手将结果从一个传递到另一个,即使是内置的查找助手也无济于事。 If you want to do so you can install ember-composable-helpers .如果你想这样做,你可以安装ember-composable-helpers Another solution that I've found on https://github.com/wycats/handlebars.js/issues/304 is to use a custom helper that will chain your calls :我在https://github.com/wycats/handlebars.js/issues/304上找到的另一个解决方案是使用自定义助手来链接您的调用:

Handlebars.registerHelper('chain', function() {
  var helpers = [];
  var args = Array.prototype.slice.call(arguments);
  var argsLength = args.length;
  var index;
  var arg;

  for (index = 0, arg = args[index];
       index < argsLength;
       arg = args[++index]) {
    if (Handlebars.helpers[arg]) {
      helpers.push(Handlebars.helpers[arg]);
    } else {
      args = args.slice(index);
      break;
    }
  }

  while (helpers.length) {
    args = [helpers.pop().apply(Handlebars.helpers, args)];
  }

  return args.shift();
});

and here is one example of call :这是调用的一个示例:

{{chain "taxAdd" "formatPrice" this.product.price}}

I hope this will help.我希望这将有所帮助。 I'll also edit your post to add the emberjs tag so that someone else could eventually give a better answer.我还将编辑您的帖子以添加 emberjs 标签,以便其他人最终可以给出更好的答案。

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

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