简体   繁体   English

用组件替换HTMLbars / Handlebars表达式中的字符串

[英]Replace string in HTMLbars/Handlebars expression with component

I've got a blog. 我有一个博客。 Each blog-posts can have multiple downloads. 每个博客帖子可以有多个下载。 For the downloads I created a component downloads Currently I render them at the end of each post like this: 对于下载,我创建了一个组件downloads目前,我在每个帖子的末尾渲染它们,如下所示:

{{#each download in sortedDownloads }}
  <p>
    <a class="dl-button" {{ action "incDownload" download }}>
      {{ download.name }} ({{ download.size}}MB)
    </a> - {{ download.downloadcount }} Hits
  </p>
{{/each}}

I'd like to be able to write something like [downloads] in the post content itself (which is simply rendered via {{{post.parsedBody}}} and replace it with a partial like the above one. 我希望能够在帖子内容本身中编写类似[downloads]的内容(只需通过{{{post.parsedBody}}}渲染,然后将其替换为上述内容的一部分即可。

Is this possible or do you have a better way to achieve this? 这可能吗,或者您有更好的方法实现这一目标?

This does not really look achievable by using either outlet or yield , since the post content will not be interpreted by the render engine. 通过使用outletyield ,这看起来确实无法实现,因为发布内容将不会由渲染引擎解释。

What should be working though is to have the placeholder in your content just as you mentioned it, and replace it by some identifiable HTML placeholder in your post.parsedBody . 但是,应该起作用的是像您提到的那样在内容中post.parsedBody占位符,并在post.parsedBody其替换为可识别的HTML占位符。 You could then create a View on didInsertElement , and call that view's appendTo() method to render the downloads inside the placeholder. 然后,您可以在didInsertElement上创建一个View ,并调用该视图的appendTo()方法在占位符内呈现下载内容。

One could say writing some jquery-ish elements also works, but I think inserting arbitrary elements in the views tree is horrible and goes against the Ember way of managing views tree. 可以说编写一些jquery-ish元素也可以,但是我认为在视图树中插入任意元素是可怕的,并且违背了Ember管理视图树的方式。

Cheers! 干杯!

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

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