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?
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}}
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.
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.
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.
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
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 ...}}
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With