dotless/dotless — Issue #135: Add support for dynamic variables (request)
Author: Strandedpirate
Created: 2012-02-24 10:07:15+00:00
State: open

At its core this request is to allow an asp.net application to dynamically create dotless variables during runtime that change per page view. My particular need is for a browser name and version variables per visit so that I could then use those variables to key off of and render css specific to that browser. I’m know there are a lot of other use cases here too.

I had several thoughts on this:
1.	Allow query string variables to be translated into dotless variables. Bad idea from attack surface perspective and would require some sort of authentication to prevent remote hacking.
a.	E.g. http://localhost/mysite/css/mysite.less?browser=IE&version=8 translates to variables @browser: “IE”; @version: 8;

2.	Allow a new file extension to be considered an official dotless variable file. E.g. myVariableFile.lessvar
a.	This is do-able and I’ve succeeded at modifying the source code for this. The problem is that dotless requires this file to be physically on disk. That makes it a static file with no way to change between thousands of page visits.

3.	The winner -> Allow .less files to not be physically on disk or .lessvar if that makes things easier. This then allows a developer to write an .ashx handler or a full blown HttpHandler that can dynamically output a file that dotless recognizes and imports/parsers just like any other .less file.
a.	Example:

Web.config setup:

DotLessVariablesHandler.ashx codebehind:

public class DotLessVariablesHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/css";
context.Response.Write("@isIE: true;");
context.Response.Write("@dynamicImage: \"ie_sucks.png\";");
}

public bool IsReusable
{
get { return false; }
}
}

MyTheme.less file:

@import "/DotLessVariables.less";

.someClass{ background: url(”images/@{dynamicImage}”) no-repeat left top white;}