Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Stefan Bergh edited this page Feb 8, 2017 · 4 revisions

To extend dotless through its plugin mechanism 3 different parts are required.

  1. A function plugin for registering functions
  2. A function implementation (or many)
  3. Configuration

Plugin

Define a named object of type IFunctionPlugin responsible for registering functions

[DisplayName("UtilPlugins")]
public class UtilPlugins : IFunctionPlugin
{
    public Dictionary<string, Type> GetFunctions()
    {
        return new Dictionary<string, Type> 
        {
            { "rnd", typeof(RandomFunction) }
        };
    }
}

Here a single function named “rnd” is registered mapping it to the RandomFunction class.

Function

Define an object of type Function and implement the Evaluate method.

public class RandomFunction : Function
{
    readonly Random random = new Random();

    protected override Node Evaluate(Env env)
    {
	Guard.ExpectNumArguments(2, Arguments.Count(), this, Location);
	Guard.ExpectNode<Number>(Arguments[0], this, Arguments[0].Location);
	Guard.ExpectNode<Number>(Arguments[1], this, Arguments[1].Location);

	var min = Arguments[0] as Number;
	var max = Arguments[1] as Number;

	return new Number(random.NextDouble() * (max.Value - min.Value) + min.Value, max.Unit);
    }
}

Here we expect exactly to arguments both of type Number. We return a Number with a random value between the two arguments. The unit of second specified Number is used for our return value.

Configuration

ASP.NET Registration

Register a plugin by adding the following to your web.config using the name of your plugin.

<?xml version="1.0"?>
<configuration>
  ...
    <dotless minifyCss="true|false" cache="true|false">
        <plugin name="UtilPlugins" assembly="Company.Plugins" />
    </dotless>
  ...
</configuration>

In code

Register a plugin by adding the plugin class to your dotless configuration.

DotlessConfiguration config = ...;
config.Plugins.Add(new GenericPluginConfigurator<UtilPlugins>());

Usage

input

margin: rnd(100px, 200px)

output

margin: 167px /* a random value between 100px and 200px */

Clone this wiki locally