How can I prevent layout from rendering by default, yet still be able to turn layout rendering on by declaring it explicitly?

zend-form
#1

I often use template->render() to generate HTML snippets. By default they are generated with a full layout. This is something I do not want. My two use cases are AJAX requests where I only need to generate a bit of text, without layout, or HTML pieces that I generate to plug into a PDF library

Usually my code may look like below, where I have to include the boilerplate of 'layout' => false in every such render method call:

$header_address = $this->template->render("app::header_address.phtml", array(
    'param' => 'param'
    'layout' => false
));
$body = $this->template->render("app::cert_body.phtml", array(
    'layout' => false
));

This is undesirable to me - it clutters up code, and most of my code does not use the layout facility. How can I make Expressive assume by default that the layout is false, and to specify it explicitly when I need it?

What is the most direct and Expressive-compatible way of doing this?

For clarify, using the above example, I want the end result to be like so, where layout is assumed to be false, unless stated otherwise:

$header_address = $this->template->render("app::header_address.phtml", array(
    'param' => 'param'
));
$body = $this->template->render("app::cert_body.phtml");
#2

We have middleware to do something very similar. Inject the renderer into it and in the process method check to see if the request is an XHR*. If it is, set your layout = false param.

Add the middleware early in your pipeline and you’re good to go.

  • not in front of my computer and can’t exactly remember the header to check.