{
    "href": "/post/2022/09/23/in-template-response-building/",
    "relId": "2022/09/23/in-template-response-building",
    "title": "In-Template Response Building",
    "author": "pmjones",
    "tags": [
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/qiq/",
            "relId": "qiq",
            "title": "Qiq",
            "author": null,
            "created": "2021-10-20 16:48:23 UTC",
            "updated": [
                "2021-10-20 16:48:23 UTC"
            ],
            "markup": "markdown"
        },
        {
            "href": "/tag/sapien/",
            "relId": "sapien",
            "title": "Sapien",
            "author": null,
            "created": "2021-11-09 16:57:39 UTC",
            "updated": [
                "2021-11-09 16:57:39 UTC"
            ],
            "markup": "markdown"
        }
    ],
    "created": "2022-09-23 12:52:28 UTC",
    "updated": [
        "2022-09-23 12:52:28 UTC",
        "2022-09-23 12:53:10 UTC",
        "2022-09-23 15:37:50 UTC",
        "2022-09-23 21:44:20 UTC",
        "2022-09-23 21:47:59 UTC",
        "2022-09-23 21:50:34 UTC",
        "2022-09-23 21:50:54 UTC",
        "2022-09-23 21:52:47 UTC",
        "2022-09-23 21:53:29 UTC",
        "2022-09-23 21:54:32 UTC",
        "2022-09-23 21:54:46 UTC",
        "2022-09-29 16:48:16 UTC",
        "2022-10-03 20:19:46 UTC",
        "2022-10-13 14:17:59 UTC"
    ],
    "markup": "markdown",
    "html": "<p>I am fond of saying that in server-side applications, <a href=\"/post/2014/05/26/the-template-is-not-the-view/\">the Template is not the View</a>. Instead, the HTTP Response is the View. That is, the Response presented by the server is not <em>only</em> the templated content; the presentation includes <em>both</em> the headers <em>and</em> the content in the Response.</p>\n<p>But what if you can build the HTTP Response <em>inside</em> a Template?</p>\n<p>That is just what the new <a href=\"https://qiqphp.com\">Qiq</a> helpers using <a href=\"https://sapienphp.com\">Sapien</a>, let you do. Using the <a href=\"https://packagist.org/packages/qiq/helper-sapien\">qiq/helper-sapien</a> package, you can set <em>Response</em> headers, cookies, etc. from inside your template code ...</p>\n<pre><code>{{ response()-&gt;setVersion('1.1') }}\n{{ response()-&gt;setCode(200) }}\n{{ response()-&gt;setHeader('content-type', 'text/plain') }}\nHello, world!\n</code></pre>\n<p>... and get back a complete <em>Response</em> object for sending:</p>\n<pre><code class=\"language-php\">$response = $template-&gt;response()-&gt;render($template);\n$response-&gt;send();\n</code></pre>\n<p>In addition, you can use <code>setFile()</code> to build a file-download Response ...</p>\n<pre><code>{{ response()-&gt;setFile('/path/to/file.txt') }}\n</code></pre>\n<p>... or <code>setJson()</code> to build a JSON Response:</p>\n<pre><code>{{ response()-&gt;setJson(['foo' =&gt; 'bar']) }}\n</code></pre>\n<p>Does this mean the Template <strong>is</strong> the View after all? If so, it's only because the entire Response is built by the Template. Perhaps this could be called a ResponseView.</p>\n<p>Will this make a Qiq template into a complete <a href=\"/adr\">ADR</a> Responder? Maybe, maybe not -- but it does mean that Responder implementations have the option to hand off most or all Response-building work to Qiq template code.</p>\n"
}
