iMasters.com - Knowledge for Developers.
Follow Us
Home Backend In-Template Response Building
Backend

In-Template Response Building

I am fond of saying that in server-side applications, the Template is not the View. Instead, the HTTP Response is the View. That is, the Response presented by the server is not only the templated content; the presentation includes both the headers and the content in the Response.

But what if you can build the HTTP Response inside a Template?

That is just what the new Qiq helpers using Sapien, let you do. Using the qiq/helper-sapien package, you can set Response headers, cookies, etc. from inside your template code …

{{ response()->setVersion('1.1') }}
{{ response()->setCode(200) }}
{{ response()->setHeader('content-type', 'text/plain') }}
Hello, world!

… and get back a complete Response object for sending:

$response = $template->response()->render($template);
$response->send();

In addition, you can use setFile() to build a file-download Response …

{{ response()->setFile('/path/to/file.txt') }}

… or setJson() to build a JSON Response:

{{ response()->setJson(['foo' => 'bar']) }}

Does this mean the Template is 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.

Will this make a Qiq template into a complete ADR 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.

Written by
Paul M. Jones

Paul M. Jones is an internationally recognized PHP professional, working in that language since 1999, and programming in general since 1983. He has held roles from junior developer to VP of Engineering in all kinds of organizations (corporate, military, non-profit, educational, medical, and others). He is a producer of programming standards, an author of certification exam questions, and a regular speaker at technical conferences worldwide.

Leave a comment

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Related Articles

Backend

How to Create a Skill for Amazon’s Virtual Assistant Alexa

If you didn’t know, it is not necessary for an Amazon Echo...

Backend

The APIs role in a 5G world

5G is about to revolutionize how we connect and use technology daily....

Backend

EF Core using AsNoTracking with Identity Resolution

Today we will see the consequences of using AsNoTracking with the Identity...

Backend

Understand key features added in ASP.NET Core 7.0

Version 7.0 of the .NET platform brought many new features in this...