While using a run time variable is not possible as a matter of principle (that would basically require compiling D code at run time), you could use a compile time defined enum
constant or template parameters to avoid using the string constants directly:
void index(string templ)(HTTPServerRequest req, HTTPServerResponse res)
{
res.render!(templ, req);
}
shared static this()
{
// ...
router.get("/", &index!"index.dt");
// BTW, this special case is equal to
//router.get("/", staticTemplate!"index.dt");
// ...
}
or using an enum
:
enum indexTemplateFile = "index.dt";
void index(HTTPServerRequest req, HTTPServerResponse res)
{
res.render!(indexTemplateFile, req);
}
This alternative form of using a named enum
can also be used:
enum TemplateFile {
index = "index.dt",
login = "login.dt"
}
void index(HTTPServerRequest req, HTTPServerResponse res)
{
res.render!(TemplateFile.index, req);
}
Both of these approaches can also be combined and the constants can even be modified with the usual string operations, as long as the result can still be computed at compile time (e.g. it is possible to call something like std.string.toLower
on the string before passing it to render
).