Conditionally using .less stylesheets - css

I'm trying to master Less and need help.
I have three stylesheets for different parts of the same application.
Lets call them 1.less, 2.less, 3.less. I also have a shared.less stylesheet, which contains some common styles for all three parts.
I want to use these files in main stylesheet application.css via require.
How can I switch between these 1.less, 2.less and 3.less in the application.css?
Like
*= require yfu_app/shared.less
*= require yfu_app/1.less when 1st part of the app is used;
*= require yfu_app/shared.less
*= require yfu_app/2.less when 2nd part of the app is used and so on.
May be there is a better way of doing it?
EDIT
Is it possible to #import less files conditionally into shared.less?

You can add condition in your layout application.html.erb to pick stylesheet as per your app page
for an example
<%= stylesheet_link_tag 'application' %>
<% if less_1 %>
<%= stylesheet_link_tag 'less1' %>
<% elsif less_2 %>
<%= stylesheet_link_tag 'less2' %>
<% elsif less_3 %>
<%= stylesheet_link_tag 'less1' %>
<% end %>

Related

Use if statement in layout.html.erb in a Sinatra app

Within my Sinatra application I would like to serve a minified version of my css/js for production but in development keep my default setup (individual files). I'm not quite sure how to write this in the correct syntax but if I give a pseudo-example, it should show what I am trying to achieve.
layout.html.erb
<% if :environment == :development %>
<!-- stylesheet link tags here -->
<% elsif :environment == :production %>
<!-- minified versions of stylesheets -->
<% end %>
is it possible to conditionally set which stylesheets I want to use, or is there a better way of doing this?
I am trying this
<% if ENV['RACK_ENV] = 'development' %>
<!-- stylesheet link tags here -->
<% elsif ENV['RACK_ENV] = 'production' %>
<!-- minified versions of stylesheets -->
<% end %>
Is there a more efficient way than this?
I have not used it yet, but it looks like sinatra-assetpack would provide an excellent solution for your problem.

How to selectively load css in rhtml?

There are several pages generated from a same layout.rhtml. Besides a global css file, each page also has its own css file, say page1.css, page2.css corresponding to page1.rhtml, page2.rhtml. Is there any way to seletively load the css files in rhtml?
Thanks in advance.
Define a instance variable in controller for which css to load. lets say we will use stylefile variable in controller like this:
#stylefile = "page1.css"
Now write following code where you load css file.
<%= stylesheet_link_tag #stylefile %>
Similarly change filename in controller for page2.
If i understood correctly,you can just include them with stylesheet_link_tag like this
<%= stylesheet_link_tag 'page1' %>
<%= stylesheet_link_tag 'page2' %>
in the .rhtml files.

Stylesheets in Rails

I would like to use one of my stylesheets from the public folder (several directories each with their own css file) for one of my controllers.
Having trouble bringing in the static css file.
I've tried the following code -
<%= stylesheet_link_tag "/public/foldername/style2.css" %>
But it's not working
Please try
<%= stylesheet_link_tag "/foldername/style2.css" %>

Using CSS in Rails

I currently have a website with a typical homepage that uses it's own css file and then the rest of the site using one other css file. Could anyone advise on the best way to tailor my Rails app to meet these requirements?
I currently have a welcome controller with a welcome css file but not sure how to link all my other controllers to a main css file.
Thanks
Just put all your css files in
app/assets/stylesheets
then in the views use:
<%= stylesheet_link_tag "the_css_file_name_with_no_extension" %>
Actually, css files and assets in general don't get associated with controllers, but with views.
I suggest you have a read to:
http://guides.rubyonrails.org/layouts_and_rendering.html
If you want to use your controllers, you can define a global variable, and use it to conditionally include different stylesheets in your application.html.erb.
Something like:
app/views/layouts/application.html.erb
<% if #css = "main" %>
<%= stylesheet_link_tag "main" %>
<% else %>
<%= stylesheet_link_tag "application" %>
<% end%>
where #css is set to "main" in your main controller.

how to use single css for multiple views?

I am creating an application where I am having many views but they all have to use a single css...
I placed my css code in assests\stylesheets\application.css
and in my view head part I am adding:
<%= stylesheet_link_tag 'application.css' %>
but stil it is not working
Try using:
<%= stylesheet_link_tag 'application' %>
Note the lack of .css
See this answer https://stackoverflow.com/a/10492888/950890

Resources