@@ -14,7 +14,7 @@ In the root directory of your Symfony project, open a terminal and enter.
1414``` shell
1515composer require pug-php/pug-symfony
1616```
17- When your are asked to install automatically needed settings, enter yes.
17+ When you are asked to install automatically needed settings, enter yes.
1818
1919It for any reason, you do not can or want to use it, you will have to add to
2020your ** config/bundles.php** file:
@@ -26,29 +26,50 @@ Pug\PugSymfonyBundle\PugSymfonyBundle::class => ['all' => true],
2626## Usage
2727
2828Create Pug views by creating files with .pug extension
29- in ** app/Resources/views ** such as contact.pug:
29+ in ** templates ** such as contact.pug:
3030``` pug
3131h1
3232 | Contact
3333 =name
3434```
3535
36- Note: standard Twig functions are also available in your pug templates, for instance:
37- ``` pug
38- !=form_start(form, {method: 'GET'})
36+ Then inject ` Pug\PugSymfonyEngine ` to call it in your controller:
37+ ``` php
38+ namespace App\Controller;
39+
40+ use Pug\PugSymfonyEngine;
41+ use Symfony\Component\HttpKernel\Attribute\AsController;
42+ use Symfony\Component\Routing\Annotation\Route;
43+
44+ #[AsController]
45+ class MyController
46+ {
47+ #[Route('/contact')]
48+ public function contactAction(PugSymfonyEngine $pug)
49+ {
50+ return $pug->renderResponse('contact/contact.pug', [
51+ 'name' => 'Us',
52+ ]);
53+ }
54+ }
3955```
4056
41- Then call it in your controller:
57+ Or alternatively you can use ` \Pug\Symfony\Traits\PugRenderer ` to call directly ` ->render() ` from
58+ any method of a controller (or service):
59+
4260``` php
4361namespace App\Controller;
4462
45- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
63+ use Pug\Symfony\Traits\PugRenderer;
64+ use Symfony\Component\HttpKernel\Attribute\AsController;
65+ use Symfony\Component\Routing\Annotation\Route;
4666
47- class MyController extends AbstractController
67+ #[AsController]
68+ class MyController
4869{
49- /**
50- * @Route("/contact")
51- */
70+ use PugRenderer;
71+
72+ #[Route('/contact')]
5273 public function contactAction()
5374 {
5475 return $this->render('contact/contact.pug', [
@@ -58,13 +79,38 @@ class MyController extends AbstractController
5879}
5980```
6081
82+ No matter if your controller extends ` AbstractController ` as it can also render twig views, so it will just
83+ work the same as before rather you ` ->render('view.html.twig') ` or ` ->render('view.pug') ` .
84+
85+ Note: standard Twig functions are also available in your pug templates, for instance:
86+ ``` pug
87+ !=form(form)
88+ ```
89+
90+ As per https://symfony.com/doc/current/forms.html
91+
92+ Pass the FormView as usual from the controller:
93+ ``` php
94+ $task = new Task();
95+ // ...
96+
97+ $form = $this->createFormBuilder($task)
98+ // ...
99+ ->getForm();
100+
101+ return $pug->renderResponse('home.pug', [
102+ 'form' => $form->createView(),
103+ ]);
104+ ```
105+
61106## Configure
62107
63108You can inject ` Pug\PugSymfonyEngine ` to change options, share values, add plugins to Pug
64109at route level:
65110
66111``` php
67112// In a controller method
113+ #[Route('/contact')]
68114public function contactAction(\Pug\PugSymfonyEngine $pug)
69115{
70116 $pug->setOptions(array(
@@ -74,14 +120,16 @@ public function contactAction(\Pug\PugSymfonyEngine $pug)
74120 ));
75121 $pug->share('globalVar', 'foo');
76122 $pug->getRenderer()->addKeyword('customKeyword', $bar);
77-
78- return $this->render ('contact/contact.pug', [
123+
124+ return $pug->renderResponse ('contact/contact.pug', [
79125 'name' => 'Us',
80126 ]);
81127}
82128```
83129
84- Same can be ran globally on a given event such as ` onKernelView ` to apply customization before any
130+ If you use the ` PugRenderer ` trait, you don't need to inject the service again and can just use ` $this->pug ` .
131+
132+ Same can be run globally on a given event such as ` onKernelView ` to apply customization before any
85133view rendering.
86134
87135See the options in the pug-php documentation: https://phug-lang.com/#options
@@ -108,7 +156,7 @@ twig:
108156
109157` ` `
110158
111- Make the translator available in every views :
159+ Make the translator available in every view :
112160` ` ` pug
113161p=translator.trans('Hello %name%', {'%name%': 'Jack'})
114162` ` `
0 commit comments