Blueprint PHP Framework Tutorial – Part 6 (Views)

Now that we know how to create models and tie them into the controller, we are going to look at how we can use views to customise the output from the model. There are two types of view built into the framework. The standard ‘View’ for rendering basic outputs and ‘PageView’ which extends ‘View’ to add a few methods for building CSS and JavaScript blocks. We will create our own view and use the ‘Table’ class to build a table to display the output from the model.

So lets go ahead a create our view class in /app/Frontend/View/Index.php:

Once again, we state the namespace and reference the Table class and PageView class from which this class is extended. The only method we are adding here is renderDataTable(). The Table class is simple enough. Take a look at /lib/Blueprint/Table/Table.php to get a feel for the methods. Use the setColumnData() method to select which keys from the $data array to show and what the column header should be labelled as for each key. Then we set the class attribute for the table.

Because the Blueprint PHP Framework makes use of Twitter’s Bootstrap CSS we are setting certain classes to make the table look prettier. Of course, you don’t have to make use of this CSS, it just saves you some time when developing if you don’t have to worry about the design too much.

Finally we call the render() method and return. Let’s update our Index controller to make use of this View class. We need to change the reference for the View to use our new class:

And update our constructor to use this class:

Finally we need to update our indexAction once more:

Also don’t forget that our model needs to return an array of data to pass to the renderDataTable method in the view, so remove the implode function and just return the $row instead.

Now, if you load the page in a browser, you should see a nicely formatted table.

Next time we’ll look at how the view loads the template and how to keep templating nice and light.

Leave a Reply