简体   繁体   中英

Why use ruby gem for bootstrap when you can just one line include bootstrap in a rails app?

Why use ruby gem for bootstrap when you can just one line include bootstrap in a rails app?

i can use bootstrap with this line in my application.html.erb file: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

Why do people use gems like https://github.com/seyhunak/twitter-bootstrap-rails ?

Without any claim of completeness, here are a few different reasons why you might want to include Bootstrap via a gem.

First, it allows you to customize the library in a much more powerful way than including it through a reference. If you browse through the linked project's README, you'll notice that you can use LESS or SASS, and overwrite individual variables within Bootstrap. This makes it much easier to create your own Bootstrap theme for example, and create clean and maintainable CSS classes.

Second, gems often provide much more functionality than just including Bootstrap. They make it easy to scaffold new pages by providing generators or overwriting templates in Rails. And they sometimes even come with custom Javascript implementations for certain Bootstrap features to make it easier to integrate them with Rails. For an example, just look at the linked library again and its helper methods for navbars:

<%= nav_bar do %>
     <%= menu_item "Home", root_path %>

    <%= drop_down "Products" do %>
        <%= menu_item "Latest", latest_products_path %>
        <%= menu_item "Top Sellers", popular_products_path %>
        <%= drop_down_divider %>
        <%= menu_item "Discount Items", discounted_products_path %>
    <% end %>

    <%= menu_item "About Us", about_us_path %>
    <%= menu_item "Contact", contact_path %>
<% end %>

That's pretty cool. Much less boilerplate code, and nothing to worry about...

Lastly, you have much more control. You are not dependent on the CDN, and can integrate Bootstrap tightly into your asset pipeline. Imo, this is a two-edged sword, though. On one hand, by excluding parts of the library you don't need, you can get a way smaller CSS file than the one provided by Bootstrap. Because it's easier to overwrite variables in Bootstrap, you're own CSS might also be significantly smaller. On the other hand, users might have already have a cached version of Bootstrap from the CDN which makes the whole effort to reduce the size of the stylesheet kinda obsolete.

There are of course downsides as well, eg that updates might take much longer to reach you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM