|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------- |
| 5 | + * |
| 6 | + * GLPI - Gestionnaire Libre de Parc Informatique |
| 7 | + * |
| 8 | + * http://glpi-project.org |
| 9 | + * |
| 10 | + * @copyright 2015-2024 Teclib' and contributors. |
| 11 | + * @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 12 | + * |
| 13 | + * --------------------------------------------------------------------- |
| 14 | + * |
| 15 | + * LICENSE |
| 16 | + * |
| 17 | + * This file is part of GLPI. |
| 18 | + * |
| 19 | + * This program is free software: you can redistribute it and/or modify |
| 20 | + * it under the terms of the GNU General Public License as published by |
| 21 | + * the Free Software Foundation, either version 3 of the License, or |
| 22 | + * (at your option) any later version. |
| 23 | + * |
| 24 | + * This program is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | + * GNU General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU General Public License |
| 30 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 31 | + * |
| 32 | + * --------------------------------------------------------------------- |
| 33 | + */ |
| 34 | + |
| 35 | +namespace Glpi\Controller; |
| 36 | + |
| 37 | +use Glpi\Application\View\TemplateRenderer; |
| 38 | +use Glpi\DependencyInjection\PublicService; |
| 39 | +use Symfony\Component\HttpFoundation\Response; |
| 40 | + |
| 41 | +abstract class AbstractController implements PublicService |
| 42 | +{ |
| 43 | + /** |
| 44 | + * Helper method to get a response containing the content of a rendered |
| 45 | + * twig template. |
| 46 | + * |
| 47 | + * @param string $view Path to a twig template, which will be looked for in |
| 48 | + * the "templates" folder. |
| 49 | + * For example, "my_template.html.twig" will be resolved to `templates/my_template.html.twig`. |
| 50 | + * For plugins, you must use the "@my_plugin_name" prefix. |
| 51 | + * For example, "@formcreator/my_template.html.twig will resolve to |
| 52 | + * `(plugins|marketplace)/formcreator/templates/my_template.html.twig`. |
| 53 | + * @param array $parameters The expected parameters of the twig template. |
| 54 | + * @param Response $response Optional parameter which serves as the "base" |
| 55 | + * response into which the renderer twig content will be inserted. |
| 56 | + * You should only use it if you need to set some specific headers into the |
| 57 | + * response or to set an http return code different than 200. |
| 58 | + * |
| 59 | + * @return Response |
| 60 | + */ |
| 61 | + final protected function render( |
| 62 | + string $view, |
| 63 | + array $parameters = [], |
| 64 | + Response $response = new Response(), |
| 65 | + ): Response { |
| 66 | + $twig = TemplateRenderer::getInstance(); |
| 67 | + |
| 68 | + // We must use output buffering here as Html::header() and Html::footer() |
| 69 | + // output content directly. |
| 70 | + // TODO: fix header() and footer() methods and remove output buffering |
| 71 | + ob_start(); |
| 72 | + $twig->display($view, $parameters); |
| 73 | + $content = ob_get_clean(); |
| 74 | + |
| 75 | + $response->setContent($content); |
| 76 | + return $response; |
| 77 | + } |
| 78 | +} |
0 commit comments