Skip to content

Commit

Permalink
Merge pull request #50 from bausshf/master
Browse files Browse the repository at this point in the history
Added ability to use a single view for routing
  • Loading branch information
bausshf authored Mar 5, 2018
2 parents 4407811 + a7e9ddb commit 91f0cdb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/webconfig.d
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static if (isWeb)
@optional string maintenance;
/// An array of ips that can still access the site during maintenance.
@optional string[] maintenanceWhiteList;
/// Boolean determining whethere there's only one view to use for routing. The view must be named __view.dd
@optional bool viewOnly;
}

/// A web address.
Expand Down
3 changes: 3 additions & 0 deletions extensions/extensiontype.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ enum ExtensionType
/// An extension used to extend the general view class.
viewExtension = "ViewExtension",

/// An extension used to extend the general view constructor.
viewCtorExtension = "ViewCtorExtension",

/// An extension used to extend the general controller class.
controllerExtension = "ControllerExtension",

Expand Down
13 changes: 12 additions & 1 deletion init/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ static if (isWebServer)

import diamond.init.web : getView;

auto page = getView(client, client.route, true);
import diamond.views.view : View;

View page;

if (webConfig.viewOnly)
{
page = getView(client, new Route("__view"), false, true);
}
else
{
page = getView(client, client.route, true);
}

if (!page)
{
Expand Down
7 changes: 7 additions & 0 deletions views/view.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ static if (!isWebApi)

_placeHolders["doctype"] = "<!DOCTYPE html>";
_placeHolders["defaultRoute"] = _client.route.name;

import diamond.extensions;
mixin ExtensionEmit!(ExtensionType.viewCtorExtension, q{
mixin {{extensionEntry}}.extension;
});

onViewCtor();
}
}
else
Expand Down
8 changes: 6 additions & 2 deletions views/viewroute.d
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static if (!isWebApi)
string generateGetView()
{
string getViewMixin = "
View getView(HttpClient client, Route route, bool checkRoute)
View getView(HttpClient client, Route route, bool checkRoute, bool keepRoute = false)
{
auto viewName =
routableViews.get(route.name, checkRoute ? null : route.name);
Expand All @@ -49,7 +49,11 @@ static if (!isWebApi)
getViewMixin ~= format(q{
case "%s":
{
client.route = route;
if (!keepRoute)
{
client.route = route;
}

return new view_%s(client, "%s");
}
}, viewName, viewName, viewName);
Expand Down

0 comments on commit 91f0cdb

Please sign in to comment.