Views / Templates

This post is part of my Dev rules of thumb series. In them, I leave you with a poster with a few rules I follow for each concept, which you can print out and scatter around your office, and a brief explanation of what that pattern is about. You can also find a PDF of the poster at the bottom of this post, for better quality printing. However, please remember, a dev rule of thumb is a Principle with broad application that is not intended to be strictly accurate or reliable for every situation.

Views / Templates

When we think of a view, we usually relate it to the MVC pattern, created in the late 1970s by Trygve Reenskaug. However that was a long time ago and software development has changed, although mostly the context. Nowadays we create much larger and complex applications than in the late 1970s, and I dare say most of them are web applications, which was not the case at that time. So what we commonly call MVC in web-development nowadays, is not what was originally created by Trygve Reenskaug.

A view, which I personally rather call template, are simple scripts containing a set of UI widgets (labels, form inputs, static text, animations, modals, etc) and the placeholders for a set of data.

These templates are part of the presentation layer, and the set of data that they present is passed on to them by the core of the application, maybe after going through some kind of transformation into a format more convenient to the template, in order to keep the template as simple as possible.

The logic in these templates is minimal, consisting only of presentation logic, ie “send a form when a button is clicked”, “show a confirmation modal before sending a form”, “make a preliminary input validation”, or “show information X, when the state is Y”.

The really important logic, the business logic, should not be in the presentation layer, it should live in the core of the application: the Application and Domain layers.

As the goal of a template is to show data that is passed on to it, it does use data, but it contains/owns no data.

I’ve created a very, very, very simple mechanism to exemplify how template rendering works. In a real production scenario, things can get very complex, with special ways of rendering special types of data, like dates or money, and escaping the data. So we should use libraries specific for this purpose. Nevertheless, for illustration purposes, here it is:

A very simple template rendering mechanism.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s