@@ -14,7 +14,7 @@ In the root directory of your Symfony project, open a terminal and enter.
14
14
``` shell
15
15
composer require pug-php/pug-symfony
16
16
```
17
- When your are asked to install automatically needed settings, enter yes.
17
+ When you are asked to install automatically needed settings, enter yes.
18
18
19
19
It for any reason, you do not can or want to use it, you will have to add to
20
20
your ** config/bundles.php** file:
@@ -26,29 +26,50 @@ Pug\PugSymfonyBundle\PugSymfonyBundle::class => ['all' => true],
26
26
## Usage
27
27
28
28
Create Pug views by creating files with .pug extension
29
- in ** app/Resources/views ** such as contact.pug:
29
+ in ** templates ** such as contact.pug:
30
30
``` pug
31
31
h1
32
32
| Contact
33
33
=name
34
34
```
35
35
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
+ }
39
55
```
40
56
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
+
42
60
``` php
43
61
namespace App\Controller;
44
62
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;
46
66
47
- class MyController extends AbstractController
67
+ #[AsController]
68
+ class MyController
48
69
{
49
- /**
50
- * @Route("/contact")
51
- */
70
+ use PugRenderer;
71
+
72
+ #[Route('/contact')]
52
73
public function contactAction()
53
74
{
54
75
return $this->render('contact/contact.pug', [
@@ -58,13 +79,38 @@ class MyController extends AbstractController
58
79
}
59
80
```
60
81
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
+
61
106
## Configure
62
107
63
108
You can inject ` Pug\PugSymfonyEngine ` to change options, share values, add plugins to Pug
64
109
at route level:
65
110
66
111
``` php
67
112
// In a controller method
113
+ #[Route('/contact')]
68
114
public function contactAction(\Pug\PugSymfonyEngine $pug)
69
115
{
70
116
$pug->setOptions(array(
@@ -74,14 +120,16 @@ public function contactAction(\Pug\PugSymfonyEngine $pug)
74
120
));
75
121
$pug->share('globalVar', 'foo');
76
122
$pug->getRenderer()->addKeyword('customKeyword', $bar);
77
-
78
- return $this->render ('contact/contact.pug', [
123
+
124
+ return $pug->renderResponse ('contact/contact.pug', [
79
125
'name' => 'Us',
80
126
]);
81
127
}
82
128
```
83
129
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
85
133
view rendering.
86
134
87
135
See the options in the pug-php documentation: https://phug-lang.com/#options
@@ -108,7 +156,7 @@ twig:
108
156
109
157
` ` `
110
158
111
- Make the translator available in every views :
159
+ Make the translator available in every view :
112
160
` ` ` pug
113
161
p=translator.trans('Hello %name%', {'%name%': 'Jack'})
114
162
` ` `
0 commit comments