Skip to content

Instantly share code, notes, and snippets.

@nightire
Last active February 21, 2017 05:34
Show Gist options
  • Save nightire/04240a8690bebbb66cf2cb752173de6c to your computer and use it in GitHub Desktop.
Save nightire/04240a8690bebbb66cf2cb752173de6c to your computer and use it in GitHub Desktop.
style attribute helper
import Ember from 'ember';
export default Ember.Component.extend({
attributeBindings: ['style']
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Helper from 'ember-helper';
import {htmlSafe} from 'ember-string';
export function style(params, hash) {
return htmlSafe(JSON.stringify(hash)
.replace(/,/g, ';')
.replace(/[{"}]/g, ''))
}
export default Helper.helper(style);
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<!-- static style, easy to understand-->
{{#example-component style="color: blue; font-size: 1em"}}
TEST
{{/example-component}}
<!-- dynamic style but hard to read and write -->
{{#with (hash color="red" font-size="1.25em") as |s|}}
{{#example-component
style=(concat "color:" s.color ";font-size:" s.font-size)}}
TEST
{{/example-component}}
{{/with}}
<!-- dynamic style with easy syntax by using helper -->
{{#with (hash color="green" font-size="2em") as |s|}}
{{#example-component
style=(style color=s.color font-size=s.font-size)}}
TEST
{{/example-component}}
{{/with}}
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "release",
"ember-template-compiler": "release",
"ember-testing": "release"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment