André Luiz André Luiz - 4 months ago 35
PHP Question

Phalcon PhP - how to disable the main layout for an action

I'm creating a action in one of my phalcon controller which will be used to generate a print version of a page. Here is my print.volt layout:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Print</title>
</head>
<body>
<!-- Begin page content -->
<div class="container">
{% block content %}{% endblock %}
</div>
</body>
</html>


And my view:

{% extends "layouts/print.volt" %}
{% block content %}
HERE
<script type="text/javascript">
window.print();
</script>
{% endblock %}


It is working, but my problem is that the content generated is inserted inside another layout that has the
{{ content() }}
tag. At the end O get a page with all the website menus, my print.volt and my view. I would like to know how can I get just the view inserted inside the print.volt, without the master layout. How can I disable this behavior?

Thanks for any help!

Answer

Two options come to mind.

1) Use Simple view, which can render a template without the layout:

$view = new \Phalcon\Mvc\View\Simple();
$view->setViewsDir('PATH_TO_YOUR_VIEWS');
$html = $view->render('template-name', $params);

This option is also really handy when generating HTML for email sending.

2) Disable the layout for the current Controller::method

$this->view->setLayout('');

3) Update: Timothy recommended an alternative to (2)

$this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);