Header Small Ahoy!

Passing Information Into Templates

In order to have our templates receive and display information from other modules, we have to do a little preparation work on our 'templates' module.

Step 1: Modify your template invoking method so that it accepts a $data array and then passes the area into the (template) view file. For example,


function public($data) {
  load("public", $data);
}

Step 2: Pass a $data array, as an argument, when calling your template from another module. For example (on Welcome.php):


function greeting() {
  $data["name"] = "David";
  $data["age"] = 21;
  $this->template("public", $data);
}


That's it! Now your data will be available from inside your template's view file. It's precisely the same mechanism as you would have with an ordinary view file.

Let's take the idea a little further. In the next page, we'll cover how pass view files (complete with dynamic information) into templates.