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

**URL:** <https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052>\
**Category:** Mezzio\
**Tags:** zend-form\
**Created:** [May 3, 2019, 3:04pm UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052 "2019-05-03T15:04:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennis-fedco](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/dennis-fedco/32/174_2.png) [@dennis-fedco](https://discourse.laminas.dev/u/dennis-fedco)\
**Post date:** [May 3, 2019, 3:04pm UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052/1 "2019-05-03T15:04:26Z")

</div>

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");
```

---

<div class="post-metadata">

**Author:** ![kynx](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kynx/32/142_2.png) [@kynx](https://discourse.laminas.dev/u/kynx)\
**Post date:** [May 3, 2019, 7:25pm UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052/2 "2019-05-03T19:25:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tyrsson](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tyrsson/32/1664_2.png) [@Tyrsson](https://discourse.laminas.dev/u/Tyrsson)\
**Post date:** [August 5, 2023, 8:36pm UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052/3 "2023-08-05T20:36:02Z")

</div>

I am aware that this is a very old topic. However, I recently ran into this same issue and since several of the solutions that I found to this problem did not work I thought I would post a working solution for the current version of Mezzio. There are probably better solutions, but this one works.  
mezzio v3.17.0  
mezzio-viewrenderer v2.12

In a piped Middleware (Mine is piped before DispatchMiddleware, just so there is no question):

```auto
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
    {
        if (! $request->hasHeader('X-Requested-With')) {
            // if we do not have this bail early
            return $handler->handle($request);
        }

        if (in_array('XMLHttpRequest', $request->getHeader('X-Requested-With'), true)) {
            $this->template->addDefaultParam(
                TemplateRendererInterface::TEMPLATE_ALL,
                'layout',
                false
            );
        }

        return $handler->handle($request);
    }

```

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [August 6, 2023, 9:35am UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052/4 "2023-08-06T09:35:18Z")

</div>

Hi @dennis-fedco,

For Ajax requests, it is recommended to use JSON View Model instead of the standard View model. You can see in this [link](https://docs.laminas.dev/laminas-view/quick-start/#creating-and-registering-alternate-rendering-and-response-strategies) how to register an alternate strategy. For disabling a layout on a particular view, you can use the setTerminal method with a false parameter like below. Thanks!

```auto
SomeController extends AbstractAction
    public function someAction(){
        //your logic
        return (new ViewModel($variablesArray))->setTerminal(true);
    }
}

```

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [August 6, 2023, 5:56pm UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052/5 "2023-08-06T17:56:24Z")

</div>

Use the [standard request field `Accept`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) instead of the [non-standard field `X-Requested-With`](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Common_non-standard_request_fields). With the [Fetch API](https://developer.mozilla.org//en-US/docs/Web/API/Fetch_API/Using_Fetch) it’s very easy.

---

<div class="post-metadata">

**Author:** ![Tyrsson](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tyrsson/32/1664_2.png) [@Tyrsson](https://discourse.laminas.dev/u/Tyrsson)\
**Post date:** [August 6, 2023, 8:42pm UTC](https://discourse.laminas.dev/t/how-can-i-prevent-layout-from-rendering-by-default-yet-still-be-able-to-turn-layout-rendering-on-by-declaring-it-explicitly/1052/6 "2023-08-06T20:42:07Z")

</div>

True, however, if you are trying to detect async request that are sent via an unknown number of javascript libraries I do not see a way around having the check for X-Requested-With. Nearly all of them that I have ever used send that field with ajax request. I do see what you mean in regards to the Accept field 🙂
