ember.js: 2.3.0: Custom template not working in Component

Hi,

Trying to do something along these lines working, but to no avail. The “I got wrapped” content never gets rendered. (This is a home-made markdown converter we have made.)

The reason why I need to use Ember.HTMLBars.compile, instead of just using a string is that we want to support data-binding within the Markdown content.

Of course, we are no longer extending Ember.view, which is no longer available, but instead doing this in an Ember.Component, like below. Could this be causing the issue? Ember.Component derives from Ember.View so I’d assume it would work, but…

export default Ember.Component.extend({
  layout: Ember.HTMLBars.compile("<div class='my-decorative-class'>{{yield}}</div>"),
  template: Ember.HTMLBars.compile("I got wrapped")
});

I cannot even get a basic example like that work, which indicates to me that the example is outdated and maybe not even working any more. (or maybe more correctly, not working from a component)

The previous code we had looked something like this. It didn’t work at all; no content was rendered at all. (The template does indeed have a {{yield}} statement in this case.)

import Ember from 'ember'
import layout from './template'

export default Ember.Component.extend
  layout: layout

  _yield: (context, options, morph, blockArguments) ->
    view = options.data.view
    markdown = (@get('text') || '').toString()
    content = marked(markdown)
    content = Ember.Handlebars.compile(content)

    view.appendChild(Ember.View,
      isVirtual: true
      tagName: ''
      layout: content
      _blockArguments: blockArguments
      _contextView: view
      _morph: morph
      context: @get('content')
    )

I know that _yield is not a very public API. 😄 I’d be happy to change this in whichever way. The ability to support data-binding within the Markdown is the critical part; just getting it rendered and displayed in the template is trivial if you skip that.

Huge thanks in advance.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

Thank you, very, very much. I am very much enjoying my developer experience.

thank you, much appreciated.

I’m not sure when this happened, but you used to be able to set layout or layoutName in an override to the component’s init method and use the specified layout, rather than the component’s template file. If, however, there is a template file defined, it overrides any attempt within the component to set the layout. I think this is backwards (i.e. broken). (Or perhaps there is a different way to override a component’s template file. If so, it would be great to document it.)

Use case: I use an addon which has a number of deeply-nested components. I do not reference the component that I need to customize within my own template files. Instead, I use a component which uses another component (which uses yet another component). So I want to reopen the deeply-nested component and adjust its template to my liking. I can easily set the layout or layoutName before calling this.super(...arguments), and the correct layout is placed within the component object, but then that is ignored by the code which actually creates the component and merges it with the magic-named template.