如何在rails应用程序中添加更少的文件

时间:2022-12-11 21:34:16

I can't seem to get this to work in my rails app. At a high level, I wan't to use LESS in my app and have it go through the asset pipeline.

在我的rails应用程序中,我似乎无法让它发挥作用。在更高的层次上,我不想在我的应用程序中减少使用,而是让它通过资产管道。

This is what my assets look like:

我的资产是这样的:

assets
|
|--- javascript
|
|--- stylesheets
            |
            |-- reset.less
            |-- my-mixins.less
            |-- my-variables.less
            |-- base.less
|
|--- images
|
|--- libs
        |
        |---foo
            |
            |--css
                |
                |--- foo-mixins.less
                |
                |--- bar.less

I would like to import "my-mixins.less", "my-variables.less" and "foo-mixins.less" into "base.less", as well as other files. "bar.less" should also be added to specific pages if needed.

我想导入“my-mixin”。少”、“变量。少”和“foo-mixins。那么“进入”基地。和其他文件一样。”酒吧。如果需要,还应该在特定页面中添加less。

How would I go about doing this?

我该怎么做呢?

3 个解决方案

#1


2  

This should work, assuming you're using the less-rails gem.

假设您使用的是不那么rails的gem,那么这应该是可行的。

First, add this to somewhere in your configuration (I have mine in my environment.rb):

首先,在你的配置中添加这个(我的配置在我的环境中。rb):

YourApp::Application.configure do
  config.less.paths << File.join(Rails.root, 'app', 'assets', 'less')
end

Now, if you have a file setup like this:

现在,如果你有这样的文件设置:

assets
|
|--- javascript
|
|--- css
|        |--- site-stylesheet.css.less
|
|--- less
         |--- my-mixins.less

You can import my-mixins in site-stylesheet by using:

您可以使用以下方法在站点样式表中导入my-mixin:

@import "my-mixins"

And import the stylesheet into your app with:

并将样式表导入您的应用程序:

<%= stylesheet_link_tag "site-stylesheet" %>

You should be able to extrapolate from there for your specific directory structure.

对于特定的目录结构,您应该能够从那里进行推断。

#2


1  

In base.css.less you can write @import "my-variables.less"; and it should work.

在base.css。少写@import“my- variabls .less”;它应该工作。

#3


0  

The files have to be in the "vendor/assets" or "vendor/plugins" (both at the root level) folder in order to reference them with relative paths.

文件必须在“供应商/资产”或“供应商/插件”(都在根级)文件夹中,以便使用相对路径引用它们。

#1


2  

This should work, assuming you're using the less-rails gem.

假设您使用的是不那么rails的gem,那么这应该是可行的。

First, add this to somewhere in your configuration (I have mine in my environment.rb):

首先,在你的配置中添加这个(我的配置在我的环境中。rb):

YourApp::Application.configure do
  config.less.paths << File.join(Rails.root, 'app', 'assets', 'less')
end

Now, if you have a file setup like this:

现在,如果你有这样的文件设置:

assets
|
|--- javascript
|
|--- css
|        |--- site-stylesheet.css.less
|
|--- less
         |--- my-mixins.less

You can import my-mixins in site-stylesheet by using:

您可以使用以下方法在站点样式表中导入my-mixin:

@import "my-mixins"

And import the stylesheet into your app with:

并将样式表导入您的应用程序:

<%= stylesheet_link_tag "site-stylesheet" %>

You should be able to extrapolate from there for your specific directory structure.

对于特定的目录结构,您应该能够从那里进行推断。

#2


1  

In base.css.less you can write @import "my-variables.less"; and it should work.

在base.css。少写@import“my- variabls .less”;它应该工作。

#3


0  

The files have to be in the "vendor/assets" or "vendor/plugins" (both at the root level) folder in order to reference them with relative paths.

文件必须在“供应商/资产”或“供应商/插件”(都在根级)文件夹中,以便使用相对路径引用它们。