Skip to content

Commit

Permalink
Custom view routes
Browse files Browse the repository at this point in the history
Added functionality to add custom routes to views as an alternative to setting routes directly in views.
  • Loading branch information
bausshf committed Jun 10, 2018
1 parent 1d97d3c commit 3201024
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/meta.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright © DiamondMVC 2018
* License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
* Author: Jacob Jensen (bausshf)
*/
module diamond.core.meta;

/// A string equivalent to the current version of Diamond.
static const diamondVersion = "2.10.1";
1 change: 1 addition & 0 deletions core/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public
import diamond.core.logging;
import diamond.core.webinitialization;
import diamond.core.cache;
import diamond.core.meta;
}
28 changes: 28 additions & 0 deletions http/routing.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ static if (isWeb)
import diamond.http.client;
import diamond.http.method;

static if (isWebServer)
{
private static __gshared string[string] _routableViews;

/**
* Adds a route to a view.
* Params:
* route = The route.
* viewName = The view name.
*/
void addViewRoute(string route, string viewName)
{
_routableViews[route] = viewName;
}

/**
* Gets the view name from a custom route.
* Params:
* route = The route used to retrieve the view name.
* Returns:
* The view name if routable, null otherwise.
*/
package(diamond) string getViewNameFromRoute(string route)
{
return _routableViews.get(route, null);
}
}

/// Collection of routes.
private static __gshared RouteEntry[string] _routes;

Expand Down
2 changes: 2 additions & 0 deletions views/viewformats.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static if (!isWebApi)
import diamond.errors.exceptions;
import diamond.controllers;
import diamond.markdown;
import diamond.core.meta;
import i18n = diamond.data.i18n;

import controllers;
Expand Down Expand Up @@ -118,6 +119,7 @@ static if (!isWebApi)
import std.conv : to;

import diamond.errors.exceptions;
import diamond.core.meta;

import models;

Expand Down
6 changes: 6 additions & 0 deletions views/viewroute.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ static if (!isWebApi)
auto viewName =
routableViews.get(route.name, checkRoute ? null : route.name);
if (!viewName)
{
import diamond.http.routing;
viewName = getViewNameFromRoute(route.name);
}
if (!viewName)
{
return null;
Expand Down

0 comments on commit 3201024

Please sign in to comment.