Take the tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm creating an unordered-list element in Backbone via underscore templating.

I've got the following:

<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;
}
share|improve this question
 
How many different height-width-image-combination to you have? Maybe you can put this in some css classes? –  mnhg Aug 13 at 12:08
 
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. –  Sean Anderson Aug 13 at 15:36
1  
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. –  cimmanon Aug 14 at 21:15
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.