Extensions


Diamond is very flexible, but sometimes it doesn't provide the exact functionality you need or perhaps you want to add additional functionality to already existing functionality within Diamond.

Diamond provides the possibility to add extensions to Diamond, which are useful, because you can modify Diamond without having to dig into Diamond's source code.

Which means your extensions and modifications will last beyond upgrades of Diamond.

Basically extensions are used to hack the core of Diamond.

To use extensions you must have an extensions.config file in your config folder.

Within the file you specify the extensions you wish to use.

Each line represents an extension entry with the format below:

ExtensionType|ExtensionName|ExtensionModule

ExtensionType


The type of the extension.

ExtensionName


The name of the extension. This name must be unique and cannot contain any symbols.

ExtensionModule


The module of the extension.



To use an extension its module code must be compiled along with your project.

A simple way to use extensions is by adding them as dependencies in your dub.json or dub.sdl.

If you're developing an extension make sure that it's compiled with targetType -> sourceLibrary.

Application Start


An extension that runs during the start-up of a Diamond project. These extensions are not supported by stand-alone.

To implement an extension for ApplicationStart the extension module must implement a public function called onApplicationStart that takes no parameters.

The extension must implement the following function:

void onApplicationStart()
{
    ...
}

Example:

module testextensions.appstarttest;

void onApplicationStart()
{
  import std.stdio;
  writeln("Hello World!");
}

And in extensions.config

ApplicationStart|appstarttest|testextensions.appstarttest

Custom Grammar


A custom grammar extension can be used to add custom grammars to Diamond.

The extension must implement the following function:

Grammar[char] createGrammars()
{
    ...
}

Part Parser


A part parser extension can be used to parse view parts.

The extension must implement the following function:

void parsePart(
  Part part,
  string viewName,
  ref string viewClassMembersGeneration,
  ref string viewConstructorGeneration,
  ref string viewModelGenerateGeneration,
  ref string viewCodeGeneration
)
{
    ...
}

View Extension


View extensions can be used to extend the general view class.

The extension must implement the following mixin template:

mixin template extension()
{
    ....
}

View Constructor


View constructor extensions can be used to extend the constructor of the general view class.

The extension must implement the following mixin template:

mixin template extension()
{
  void onViewCtor()
  {
    ...
  }
}

Controller


Controller extensions can be used to extend the general controller class.

The extension must implement the following mixin template:

mixin template extensions()
{
    ...
}

Http Settings


Http settings extensions can be used to extend the initial settings for Diamond.

The extension must implement the following function:

void handleSettings(HTTPServerSettings settings)
{
    ...
}

or

void handleSettings(ref HTTPServerSettings settings)
{
    ...
}

Http Request


Http request extensions can be used to handle requests.

Essentially the extension is equivalent to the function used in web settings for onBeforeRequest, except for that onBeforeRequest will respond with badRequest when returning false.

The extension must implement the following function:

bool handleRequest(HTTPServerRequest request, HTTPServerResponse response)
{
    ...
}

Note: if the function returns true the request will be handled as normal.

Error Handling


Handle error extensions can be used to handle errors when encountered.

The extension must implement the following function:

bool handleError(HTTPServerRequest request, HTTPServerResponse response, HTTPServerErrorInfo error)
{
    ...
}

Note: If the function returns true the error will be handled normally.

Static Files


Static file extensions can be used to handle static file requests.

The extension must implement the following function:

bool handleStaticFile(HTTPServerRequest request, HTTPServerResponse response)
{
    ...
}

Note: If the function returns true the static file request will be handled normally.