简体   繁体   English

当您仅将一行代码包含在Rails应用程序中时,为什么将ruby gem用于引导程序?

[英]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? 当您仅将一行代码包含在Rails应用程序中时,为什么将ruby gem用于引导程序?

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"> 我可以在我的application.html.erb文件中的这一行使用引导程序: <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 ? 人们为什么使用像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. 没有任何完整性的声明,以下是您可能要通过gem包含Bootstrap的几种不同原因。

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. 如果浏览链接项目的自述文件,您会注意到可以使用LESS或SASS,并覆盖Bootstrap中的各个变量。 This makes it much easier to create your own Bootstrap theme for example, and create clean and maintainable CSS classes. 例如,这使创建自己的Bootstrap主题以及创建干净且可维护的CSS类变得更加容易。

Second, gems often provide much more functionality than just including Bootstrap. 其次,gem通常提供的功能远远超过仅包含Bootstrap的功能。 They make it easy to scaffold new pages by providing generators or overwriting templates in Rails. 通过在Rails中提供生成器或覆盖模板,它们使搭建新页面变得容易。 And they sometimes even come with custom Javascript implementations for certain Bootstrap features to make it easier to integrate them with Rails. 而且它们有时甚至带有针对某些Bootstrap功能的自定义Javascript实现,以使其更易于与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. 您不依赖CDN,可以将Bootstrap紧密集成到资产管道中。 Imo, this is a two-edged sword, though. Imo,不过,这是一把两刃剑。 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. 一方面,通过排除不需要的库部分,可以获得比Bootstrap提供的CSS文件更小的文件。 Because it's easier to overwrite variables in Bootstrap, you're own CSS might also be significantly smaller. 因为在Bootstrap中覆盖变量更容易,所以您自己的CSS也可能要小得多。 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. 另一方面,用户可能已经从CDN获得了Bootstrap的缓存版本,这使整个工作减少了样式表的大小。

There are of course downsides as well, eg that updates might take much longer to reach you. 当然也有缺点,例如,更新可能需要更长的时间才能到达。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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