Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use url parameters in LESS css?

Intro:

I'm trying out LESS in an asp.net mvc environment.

I use dotless for server side processing (and I wouldn't want to use client side processing especially afer publishing the complete project).

I have to apply a design where there are different color schemes depending on different things (e.g. time of the day).

Less felt very powerful in this case as designing a parameterized css and only changing like 10 variables at the beginning of the file for every theme was really uplifting.

Problem:

But I would need to somehow change the color themes from an outside parameter.

Ideas:

First I thought that an URL parameter like style.less?theme=fuschia would be good, but I found no way to parse something like this.

Then I thought that making a very short blue.less, green.less, orange.less consisting only declared color variables, and including the main.less in every one of them would be a solid solution.

I had no chance to try out the second solution, but I thought this would be a good time to ask for advice on the most robust way of doing this.

The problem again is: I want to control some things in my less file from the outside.

like image 668
vinczemarton Avatar asked Feb 11 '12 09:02

vinczemarton


People also ask

Can I pass a URL as parameter?

A URL parameter is a way to pass data in a link to a web page. You can use URL parameters to track different metrics in services like Google Analytics or Google Ads, and in Unbounce to pre-fill your form.

How do you pass a variable in LESS?

LESS gets compiled into static CSS, so there's no way to access variables or functions at runtime. You can either use javascript to assign new values to specific CSS properties with element. style. (property) , or use CSS native variables which are pretty new thing that allows to be modified at runtime.

How do I pass multiple URL parameters?

To add a parameter to the URL, add a /#/? to the end, followed by the parameter name, an equal sign (=), and the value of the parameter. You can add multiple parameters by including an ampersand (&) between each one.

How many parameters can a URL have?

1000 parameters is the maximum by default. This default can be customized in the HTTP Protocol Validation Policy. An attacker would use exceptionally long parameter names or values for three different purposes: To launch an overflow attack against the data structure that stores the parameters as name-value pairs.


2 Answers

Yes you can (because I implemented that feature for exactly that reason).

Dotless supports parameters from the outside via the querystring parameter.

<link rel="stylesheet" href="style.less?foo=bar" />

Will let you use the following less:

@foo = bar;

The parameter injection code is very simple. it just prepends the variable declarations to your normal less file, so anything that comes as a querystring parameter will follow the above syntax.

The code in question is very simple: https://github.com/dotless/dotless/blob/master/src/dotless.Core/Engine/ParameterDecorator.cs

like image 121
Tigraine Avatar answered Sep 30 '22 04:09

Tigraine


AFAIK, you cannot pass parameters for dotnetless to use to do the compile.

As a suggestion, why not just call different less files? This would be fairly easy to do by using a Viewbag property.

To make the different less ones, You first create a less file with each set of colors in them. Then you import your base css file. dotnetless will merge the color definations in the parent file with the usages in the base file. So you have something like -

@baseGray: #ddd;
@baseGrayDark: darken(@baseGray, 15%);
@baseGrayLight: lighten(@baseGray, 10%);
@import "baseCss.less";

I just tested this on and MVC3 project and it works.

like image 44
photo_tom Avatar answered Sep 30 '22 04:09

photo_tom