Skip to content

Commit

Permalink
Merge pull request #11 from bausshf/master
Browse files Browse the repository at this point in the history
Base-view passing to controllers
  • Loading branch information
bausshf authored Oct 25, 2017
2 parents 131ef6a + 92db88c commit d0cfe60
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions controllers/controller.d
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ static if (isWeb)
// WebServer's will have a view associated with the controller, the view then contains information about the request etc.
static if (isWebServer)
{
public import diamond.views.view;

/// Wrapper around a controller.
class Controller(TView) : BaseController
{
Expand Down
31 changes: 31 additions & 0 deletions views/view.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ static if (!isWebApi)
import diamond.http;
}

/**
* Template to get the type name of a view.
* Params:
* name = The name of the view.
*/
template ViewTypeName(string name)
{
mixin("alias ViewTypeName = view_" ~ name ~ ";");
}

/// The abstract wrapper for views.
abstract class View
{
Expand Down Expand Up @@ -289,6 +299,27 @@ static if (!isWebApi)
append(result);
}

/**
* Gets th current view as a specific view.
* Params:
* name = The name of the view to get the view as.
* Returns:
* The view converted to the specific view.
*/
auto asView(string name)()
{
mixin("import diamondapp : getView, view_" ~ name ~ ";");

static if (isWebServer)
{
mixin("return cast(view_" ~ name ~ ")this;");
}
else
{
mixin("return cast(view_" ~ name ~ ")this;");
}
}

/**
* Retrieves a raw view by name.
* This wraps around getView.
Expand Down
4 changes: 2 additions & 2 deletions views/viewformats.d
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ static if (!isWebApi)
};

/// The format for the controller member.
enum controllerMemberFormat = "%s!view_%s controller;\r\n";
enum controllerMemberFormat = "%s!%s controller;\r\n";

/// The format for controller constructors.
enum controllerConstructorFormat = "controller = new %s!view_%s(this);\r\n";
enum controllerConstructorFormat = "controller = new %s!%s(this);\r\n";

}
else
Expand Down
14 changes: 12 additions & 2 deletions views/viewparser.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static if (!isWebApi)

import std.string : strip, format;
import std.array : replace, split;
import std.conv : to;

/**
* Parses the view parts into a view class.
Expand All @@ -34,6 +35,7 @@ static if (!isWebApi)
string viewCodeGeneration = "";
string viewPlaceHolderGeneration = "";
bool hasController;
bool useBaseView;
string layoutName = null;

foreach (sectionName,parts; allParts)
Expand Down Expand Up @@ -91,6 +93,7 @@ static if (!isWebApi)
viewName,
viewClassMembersGeneration, viewConstructorGeneration,
viewModelGenerateGeneration, viewPlaceHolderGeneration,
useBaseView,
hasController,
layoutName, route
);
Expand Down Expand Up @@ -185,6 +188,7 @@ static if (!isWebApi)
ref string viewConstructorGeneration,
ref string viewModelGenerateGeneration,
ref string viewPlaceHolderGeneration,
ref bool useBaseView,
ref bool hasController,
ref string layoutName,
ref string route)
Expand Down Expand Up @@ -236,11 +240,17 @@ static if (!isWebApi)

static if (isWebServer)
{
case "controllerUseBaseView":
{
useBaseView = to!bool(value);
break;
}

case "controller":
{
hasController = true;
viewClassMembersGeneration ~= controllerMemberFormat.format(value, viewName);
viewConstructorGeneration ~= controllerConstructorFormat.format(value, viewName);
viewClassMembersGeneration ~= controllerMemberFormat.format(value, useBaseView ? "View" : "view_" ~ viewName);
viewConstructorGeneration ~= controllerConstructorFormat.format(value, useBaseView ? "View" : "view_" ~ viewName);
break;
}
}
Expand Down

0 comments on commit d0cfe60

Please sign in to comment.