From 32010245338b7e309ee1943c51ac99e922ab84f7 Mon Sep 17 00:00:00 2001 From: Jacob Jensen Date: Sun, 10 Jun 2018 04:07:25 +0200 Subject: [PATCH] Custom view routes Added functionality to add custom routes to views as an alternative to setting routes directly in views. --- core/meta.d | 9 +++++++++ core/package.d | 1 + http/routing.d | 28 ++++++++++++++++++++++++++++ views/viewformats.d | 2 ++ views/viewroute.d | 6 ++++++ 5 files changed, 46 insertions(+) create mode 100644 core/meta.d diff --git a/core/meta.d b/core/meta.d new file mode 100644 index 0000000..455470b --- /dev/null +++ b/core/meta.d @@ -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"; diff --git a/core/package.d b/core/package.d index 8e3f8af..0f37d4a 100644 --- a/core/package.d +++ b/core/package.d @@ -19,4 +19,5 @@ public import diamond.core.logging; import diamond.core.webinitialization; import diamond.core.cache; + import diamond.core.meta; } diff --git a/http/routing.d b/http/routing.d index ba86439..901b2b5 100644 --- a/http/routing.d +++ b/http/routing.d @@ -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; diff --git a/views/viewformats.d b/views/viewformats.d index 1518310..14e9227 100644 --- a/views/viewformats.d +++ b/views/viewformats.d @@ -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; @@ -118,6 +119,7 @@ static if (!isWebApi) import std.conv : to; import diamond.errors.exceptions; + import diamond.core.meta; import models; diff --git a/views/viewroute.d b/views/viewroute.d index 166226d..3ab02c4 100644 --- a/views/viewroute.d +++ b/views/viewroute.d @@ -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;