The EmployeeController class

The EmployeeController class will be the embodiment of the web framework. It will also be the controller for linking our views with the data (the models).

Edit source\empcontrol.d:

module empcontrol;
import vibe.vibe;
import empmodel;

class EmployeeController
{
  private EmployeeModel empModel;
  private string realm = "The Lorem Ipsum Company";
  
  this()
  {
    empModel = new EmployeeModel;
  }
  
  void index()
  {
    render!"index.dt";
  }
  
  void getAddEmployee()
  {
    render!("empadd.dt", departments, provinces, paygrades);
  }
}

In D, the constructor is this() while the destructor is ~this(). In the constructor, we instantiated an empModel variable so we can start using the EmployeeModel class.

The method

render!"index.dt";

is a variadic function template. It means render!() can have a varying number of parameters (arguments) of different data types. We have seen this construct before with staticTemplate!(). In D, if there is only one parameter, the parentheses are optional.

In this case,

render!("empadd.dt", departments, provinces, paygrades);

render!() has three arguments (or parameters), one string and three arrays, so we have to enclose them in parentheses.

Let us test by running the app.

Sometimes, instead of seeing a successful compilation, you might see several lines of errors instead. The usual culprit is the indentation. In that case, click on the ‘Tab Size:’ prompt at the bottom of VS Code for each file that cause an error:

And choose to either convert all tabs to spaces or all spaces to tabs from the choices at the top of VS Code to make indentations uniform and consistent and make sure all indentation rules are followed, then save the file again.

We indicated that we are going to use an EmployeeModel class which we haven’t created yet, so let’s do that next.

Last updated