Render template by name into string

Hi guys,

I know how to render a template by name into a view like this:

// in a router
this.render("template-name", {"into": "view"}

and I know how to compile a template string and return to a variable like this:

Handlebars.compile("template content");

but I don’t know how to do both at the same, for example:

var templateString = loadTemplateByName("template-name"):
var rendered = HandleBars.compile(templateString);

any idea?

Why do you want to do this? What are you trying to achieve?

I am writing a Handlebars helper that will replace its content in case there is no permission. The replacemente is for a named template (which template, it can vary). Like below (it is simplified):

template

{{#feature "crm.business"}}
  Content if there is permission for that feature.
{{/feature}}

helper code

Handlebars.registerHelper("feature", function(featureName, content){
  var _this = this;
  if (App.Features.hasPermission(featureName))
    return content.fn(_this)
  else
    return ficticiousRenderFunction(App.Features.features[featureName].templateName);
});

as I said, the code above is simplified to apply example. There are other details but they don’t matter for this case.

Thanks :slight_smile:

Prior to v1.8.0.beta.1, I was able to do the following in a component:

var view = MyView.create(context: context);
view.container = this.get('targetObject.container');
var buffer = Ember.RenderBuffer();
view.renderToBuffer(buffer); /* Note: private method */
var html = buffer.toString();

However Ember.View#renderToBuffer was removed in 74b4330e68a2c1e7cbd009bdd1e4798142a8fa99. Haven’t figured out a replacement yet.

In my case, I’m using this to render a template into a string to pass into a non-ember plugin, selectize, to do custom rendering of items in the select list.

Snippet from here:

function templateToString(template) {
  var View = Ember.View.create({ template: template });
  var buffer = new Ember.RenderBuffer();

  view.render(buffer)

  return buffer.string();
}