I'm creating an unordered list element in Backbone via Underscore templating:
<ul id="FrontRack" style="
background-image: url({%= data.get('racks').at(0).get('imageUrls')[0] %});
height:{%= data.get('racks').at(0).get('pixelHeight') %}px;
width:{%= data.get('racks').at(0).get('pixelWidth') %}px;
">
<li>Hello World</li>
</ul>
I'm wondering two things:
- Is setting
style
still considered bad practice when creating elements through templating? - Should I be setting the style using jQuery after I generate the HTML from the template? This would be done inside of Backbone's render instead of inside of the template.
Something like:
render: function () {
this.$el.html(this.template(this.model.toJSON()));
this.$el('#FrontRack').css({ backgroundImage: ... }); // Example
return this;
}
-
1\$\begingroup\$ How many different height-width-image-combination to you have? Maybe you can put this in some css classes? \$\endgroup\$mheinzerling– mheinzerling2013年08月13日 12:08:41 +00:00Commented Aug 13, 2013 at 12:08
-
\$\begingroup\$ The height/width is being pulled from a customer database. The customer uploads images which fit the dimensions of their racks. So, the height and width could be any size. \$\endgroup\$Sean Anderson– Sean Anderson2013年08月13日 15:36:45 +00:00Commented Aug 13, 2013 at 15:36
-
5\$\begingroup\$ If the images can be any size, it is impractical to use classes. So yes, it would be appropriate to use inline styles for truly dynamic content. Keep in mind that client side languages are slow (eg. you'll have to wait for the image to load to figure out its dimensions), so anything you can do serverside is going to have better performance from the user's perspective. \$\endgroup\$cimmanon– cimmanon2013年08月14日 21:15:58 +00:00Commented Aug 14, 2013 at 21:15
1 Answer 1
Interesting questions.
Is setting 'style' still considered bad practice when creating elements through templating?
Only if you cannot reasonably use css classes (which is your case)
Should I be setting the style using jQuery after I generate the HTML from the template?
Since you know the height and size prior to generating, you should make this part of the template
Also, this :
<ul id="FrontRack" style="
background-image: url({%= data.get('racks').at(0).get('imageUrls')[0] %});
height:{%= data.get('racks').at(0).get('pixelHeight') %}px;
width:{%= data.get('racks').at(0).get('pixelWidth') %}px;
">
<li>Hello World</li>
</ul>
Only accesses data under data.get('racks').at(0)
, so that is what you should provide to the template ( as say, data
), then your template becomes
<ul id="FrontRack" style="
background-image: url({%= data.get('imageUrls')[0] %});
height:{%= data.get('pixelHeight') %}px;
width:{%= data.get('pixelWidth') %}px; ">
<li>Hello World</li>
</ul>
-
\$\begingroup\$ @SeanAnderson, glad to see you are still around and involved in CR. Thanks for accepting this answer. \$\endgroup\$Malachi– Malachi2014年03月28日 14:15:17 +00:00Commented Mar 28, 2014 at 14:15
Explore related questions
See similar questions with these tags.