{
    "href": "/post/2014/05/26/the-template-is-not-the-view/",
    "relId": "2014/05/26/the-template-is-not-the-view",
    "title": "The Template Is Not The View",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/adr/",
            "relId": "adr",
            "title": "Action Domain Responder",
            "author": null,
            "created": "2020-08-17 21:07:42 UTC",
            "updated": [
                "2020-08-17 21:07:42 UTC",
                "2020-09-22 15:41:16 UTC",
                "2020-10-14 18:20:29 UTC",
                "2020-10-14 18:36:31 UTC",
                "2020-10-14 18:36:53 UTC",
                "2020-10-14 18:37:08 UTC",
                "2020-10-14 18:37:48 UTC",
                "2020-10-14 18:39:26 UTC",
                "2020-10-14 19:03:17 UTC",
                "2020-10-14 19:03:35 UTC",
                "2020-10-26 18:12:53 UTC"
            ],
            "markup": "markdown"
        },
        {
            "href": "/tag/patterns/",
            "relId": "patterns",
            "title": "Patterns",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2014-05-26 16:23:18 UTC",
    "updated": [
        "2014-05-26 16:23:18 UTC"
    ],
    "html": "<p>As server-side web developers, we tend to think of our templating system as providing the View layer in a <a href=\"http://www.martinfowler.com/eaaCatalog/modelViewController.html\">Model-View-Controller</a> architecture.  I have recently come to the conclusion that this is an incorrect approach.  Instead, we need to think of the HTTP response as the View.</p>\n<p>When an HTTP client makes an HTTP request, what does it get in return?  It does not receive <em>only</em> the body of the HTTP response. The client receives the body <em>and</em> the headers of the HTTP response. The response as a whole is what the web application delivers as its presentation to the client.</p>\n<p>Templating systems, as far as I can tell, deal only with the body of the HTTP response. In practice, the rest of the response is generally handled by the Controller; for example, setting the status, setting cookies, and modifying headers.</p>\n<p>Let's say we accept the idea that the HTTP response is the View (i.e., the presentation that is delivered to the client). If we accept that idea, then for a clean separation of concerns a la <a href=\"http://martinfowler.com/eaaDev/SeparatedPresentation.html\">SeparatedPresentation</a>, we need to combine <em>both</em> the template work <em>and</em> the header work into their own layer. That layer needs to be completely separated from the Controller and Model.</p>\n<p>Thus, anything that deals with the response, including the setting of headers, should be considered the View layer.  If you are setting headers in a Controller, you are losing that clean separation of concerns.  To remedy this, your Controller needs to hand off to <a href=\"https://github.com/pmjones/adr/blob/master/ADR.md#view-versus-responder\">a layer that builds the response on its own</a>; this is the proper place for the tempalting system to be invoked, not the Controller.</p>\n<p>In summary: the template is not the View.  The <em>response</em> is the View.  We should separate our concerns accordingly.</p>\n<h3>\n<a name=\"user-content-afterword\" class=\"anchor\" href=\"#afterword\"><span class=\"octicon octicon-link\"></span></a>Afterword</h3>\n<p>If you like the line of thinking presented here, please check out the <a href=\"http://pmjones.io/adr\">Action-Domain-Responder</a> refinement of the MVC pattern.  It's still a work-in-progress so be sure to leave your comments and criticism either here on this blog or as a new issue at Github.</p>\n<hr>\n<p class=\"reddit-links\">Read the Reddit discussions about this post <a href=\"https://www.reddit.com/r/webdev/comments/26j5o9/the_template_is_not_the_view_xpost_from_rphp/\">here</a> and <a href=\"https://www.reddit.com/r/PHP/comments/26j3nf/the_template_is_not_the_view/\">here</a>.</p>\n"
}
