Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars helper to return markup

I have the following very simple handlebars helper that returns an image url:

Ember.Handlebars.registerHelper 'avatarUrl', (property, options) ->
  if value = Ember.get(this, property)
    if small_url = value.small_url
      return small_url

  '/images/fallback/small_default.png'

Which I use like this:

<img src="{{avatarUrl avatar}}" title="displayName">

The above works but what I would like to do is return the entire img element.

Is this possible with a handleabars helper?

like image 270
dagda1 Avatar asked Jan 21 '13 22:01

dagda1


People also ask

What is helper in Handlebars?

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}

How do you make Handlebars for helpers?

To create custom helpers in your theme just create a helpers. js file in the theme directory; here you can add your custom helper functions. Below there is an example of code which creates two super simple custom helpers which accept one argument: /* * Custom theme helpers for Handlebars.

What are Handlebars options?

When you use this syntax, Handlebars passes an options parameter to your helper as the last argument. The options object contains a fn method that works just like a compiled template... var html = options. fn(context); gives you the rendered template from inside the block.

What does variable triple brace meaning in Handlebars?

Because it was originally designed to generate HTML, Handlebars escapes values returned by a {{expression}} . If you don't want Handlebars to escape a value, use the "triple-stash", {{{ . Source: https://handlebarsjs.com/guide/#html-escaping.


3 Answers

import { htmlSafe } from '@ember/string'; for Ember 3 https://guides.emberjs.com/v3.0.0/templates/writing-helpers/#toc_escaping-html-content

safeString was deprecated in 2.8.0 and removed with 3.0.0 https://emberjs.com/deprecations/v2.x/#toc_use-ember-string-htmlsafe-over-ember-handlebars-safestring

like image 65
givanse Avatar answered Oct 05 '22 06:10

givanse


From the fine manual (right at the bottom):

If your helper returns HTML that you do not want escaped, make sure to return a new Handlebars.SafeString.

So if you want your helper to return a fully formed <img> element, then build your HTML string and wrap it in a Handlebars.SafeString:

Ember.Handlebars.registerHelper 'avatarImg', (property, options) ->
  # Build your <img> HTML string and leave it in result ...
  new Handlebars.SafeString(result)

and then in your template you should be able to have:

blah blah {{avatarImg ...}} blah blah

and get an <img> out of your {{avatarImg ...}}.

like image 14
mu is too short Avatar answered Oct 12 '22 18:10

mu is too short


You can create a view to represent that image tag and then call handlebars view helper from within your custom helper. For example:

App.AvatarView = Ember.View.extend({
  tagName: 'img',
  attributeBindings: ['src']
});

Ember.Handlebars.registerHelper('avatarUrl', function(property, options) {
  var small_url, value;
  value = Ember.get(this, 'avatarUrl');
  if (value) {
    small_url = value.small_url;
  }
  var hash = options.hash;
  hash.src = small_url || '/images/fallback/small_default.png';

  Ember.Handlebars.helpers.view.call(this, App.AvatarView, options);

});

I've posted a working example here: http://jsbin.com/adewot/1/edit

This is based on the way ember's own linkTo helper works. To see a more advanced example check out the source here: https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/helpers/link_to.js#L83

like image 2
Mike Grassotti Avatar answered Oct 12 '22 19:10

Mike Grassotti