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

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.

Related

Disabling JS/CSS bundling if debug=true in Web.config

I am using BundleTransformer to bundle and minify JS/CSS in a legacy ASP.NET WebForms app. However, I'd like to disable bundling if <compilation debug="true"> in Web.config.
Current Behavior: If debug=true, resources are still bundled together, but nothing is minified.
Desired Behavior: Bundling and minification only occur if debug=false. Otherwise, references to the individual scripts and stylesheets are rendered in the HTML.
Is this possible?
Below is an example of how I currently create one of the bundles:
BundleConfig.cs
var jsMinifier = new BundleTransformer.JsMin.Minifiers.CrockfordJsMinifier();
var customJsTransformer = new BundleTransformer.Core.Transformers.ScriptTransformer(jsMinifier);
var CustomJsBundle = new BundleTransformer.Core.Bundles.CustomScriptBundle("~/bundles/CustomJsBundle");
foreach (var item in jsFiles)
{
CustomJsBundle.Include(item);
}
CustomJsBundle.Transforms.Clear();
CustomJsBundle.Transforms.Add(customJsTransformer);
BundleTable.Bundles.Add(CustomJsBundle);
Navigation.master
<%# Master Language="C#" CodeFile="Navigation.master.cs" Inherits="Main_Nav" %>
<%# Import Namespace= "System.Web.Optimization" %>
<html>
<head runat="server">
<%= Scripts.Render("~/bundles/CustomJsBundle") %>
<!-- if debug=true, I'd like the server to just render references to the individual script files
instead of the bundle -->
</head>
<body>
<!-- other markup -->
</body>
</html>
The Scripts.Render call in Navigation.master always displays a bundle, even if debug is true. I'd like it to render the bundle only if debug is false. Otherwise, I'd like script tags to each individual script file rendered instead for debugging purposes.
I solved this for now by using the RenderFormat method when in DEBUG mode. Otherwise, I use the usual Render method.
For example, in my Navigation.master:
<% #if DEBUG %>
<%= Scripts.RenderFormat("<script src='{0}'/></script>", "~/bundles/CustomJsBundle") %>
<% #else %>
<%= Scripts.Render("~/bundles/CustomJsBundle") %>
<% #endif %>
This has the desired effect of:
In DEBUG mode, render script tags with the individual files.
Otherwise, a single script tag with the bundle is rendered.
I'm not sure if there is a better way to do this, but if so, please comment or submit an answer. I tried other potential solutions, such as setting EnableOptimizations to false, and clearing the BundleTable, but this is the only solution that has worked.

Conditionally using .less stylesheets

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 %>

Rails 5 - User specified CSS not working on Heroku with custom domain

I'd like to add a way for users to specify a few colors to be used on the site rather than the base ones from my core CSS. I've accomplished this via the code below, however it is not working in my Heroku hosted production environment, specifically the one with a custom domain and DNS routed through Cloudflare (i.e. it works on my-app.herokuapp.com but not www.my-custom-domain.com). If I open the custom CSS file directly in my browser (via the page source) and refresh it then the CSS is reflected on the site. Is this a Heroku limitation? Cloudflare? Is my approach less than ideal?
views/layouts/application.html.erb:
<html>
<head>
...
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => 'reload' %>
<% if the user is signed in and has selected custom colors %>
<%= stylesheet_link_tag(application_custom_colors_path(format: :css), media: 'all', 'data-turbolinks-track' => 'reload' ) %>
<% end %>
...
</head>
</html>
application_controller.rb
def custom_colors
#color1 = current_user.color1
#color2 = current_user.color2
respond_to do |format|
format.css
end
end
views/application/custom_colors.css.erb
body {
color: <%= #color1 %>;
}
As I said above, this works on my local machine and deployed to Heroku using their standard my-app.herokuapp.com domain, however it ceases to work when using a custom domain via Cloudflare. Any guidance is much appreciated.
page source:
<link rel="stylesheet" media="all" href="/assets/application-3fac9a8b9a23a84ee912eb6f5438eff5514038c3646ef0a547ebb512994b6ca4.css" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/application/custom_colors.css" data-turbolinks-track="reload" />
Turns out Cloudflare caches all static content with certain extensions, including CSS and Javascript, by default. To get around this I added a Page Rule in my Cloudflare account to bypass the caching of www.mycustomdomain.com/application/custom_colors.css. More info here.

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.

ASP.NET MVC Modify CSS links via code (runtime/development)?

does anyone know of a good way of doing the following, I need to have available via CSS Links up to 5 CSS files in one, this is pretty bad for trips to the server.. but it helps me keep it organized...
At runtime i automatically consolidate and minify all css files into 1 ...
What i was wondering is how to have links to css files in my source at design time and different at runtime??
Is there some special workaround i can do with <% %> tags?
Thanks
You could have one link to the combined file in your HTML and use a build event to combine your separate ones in to the one file. That way, during dev you always see your neat separate files but the designer (and at runtime) will always see the combined file. The designer doesn't care of there is one or 5 files but you do. This way you don't have to do any conditional design-time only logic and your code will be cleaner.
I use if (false) with HtmlHelper extensions to achieve similar effects. It might look like:
<% if (false) { %>
<link href="../content/styles/jquery-ui.css" ...
<link href="../content/styles/site.css" ...
<% } %>
<%= Html.CSS( "jquery-ui.css", "site.css", ... ) %>
You can try the following in your view or master page:
1) Leave the min'ed CSS links in as they are
2) Use this conditional block to include the CSS files directly as needed for design time:
<% if (this.DesignMode) { %>
<link rel="stylesheet" type="text/stylesheet" href="css/styles.css" />
<% } %>

Resources