@@ -395,17 +395,29 @@ use the ``render()`` helper::
395
395
namespace App\Controller;
396
396
397
397
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
398
+ use Symfony\Component\HttpFoundation\Response;
398
399
399
400
class ProductController extends AbstractController
400
401
{
401
402
public function index()
402
403
{
403
404
// ...
404
405
406
+ // the `render()` method returns a `Response` object with the
407
+ // contents created by the template
405
408
return $this->render('product/index.html.twig', [
406
409
'category' => '...',
407
410
'promotions' => ['...', '...'],
408
411
]);
412
+
413
+ // the `renderView()` method only returns the contents created by the
414
+ // template, so you can use those contents later in a `Response` object
415
+ $contents = $this->renderView('product/index.html.twig', [
416
+ 'category' => '...',
417
+ 'promotions' => ['...', '...'],
418
+ ]);
419
+
420
+ return new Response($contents);
409
421
}
410
422
}
411
423
@@ -446,19 +458,6 @@ the :class:`Twig\\Environment` class::
446
458
}
447
459
}
448
460
449
- Rendering a Template's Content
450
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
451
- The ``renderView() `` method renders a template and returns its content. The content from the template can be used to create a ``Response `` object::
452
-
453
- use Symfony\Component\HttpFoundation\Response;
454
-
455
- $content = $this->renderView('product/index.html.twig', [
456
- 'category' => '...',
457
- 'promotions' => ['...', '...'],
458
- );
459
-
460
- return new Response($content);
461
-
462
461
Rendering a Template in Emails
463
462
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
464
463
0 commit comments