Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

Application modulaire fonctionnelle !

Emmanuel ROY authored on 12/08/2019 15:10:25
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,371 +0,0 @@
1
-# Windwalker Renderer
2
-
3
-Windwalker Renderer is a simple template engine loader to load file for engines to render.
4
-
5
-## Installation via Composer
6
-
7
-Add this to the require block in your `composer.json`.
8
-
9
-``` json
10
-{
11
-    "require": {
12
-        "windwalker/renderer": "~3.0"
13
-    }
14
-}
15
-```
16
-
17
-## Support Engines
18
-
19
-- [PHP](#getting-started)
20
-- [Twig](#twig-renderer)
21
-- [Blade](#blade-renderer)
22
-- [Edge](#edge-renderer) (A Blade compitable engine without dependencies)
23
-- [Mustache](#mustache-renderer)
24
-- [Plates](#plates-renderer)
25
-
26
-## Getting Started
27
-
28
-``` php
29
-use Windwalker\Renderer\PhpRenderer;
30
-
31
-$config = array();
32
-
33
-$renderer = new PhpRenderer(__DIR__ . '/file/path', $config);
34
-
35
-$data = array('title' => 'foo');
36
-
37
-echo $renderer->render('template', $data);
38
-```
39
-
40
-### In `template.php`
41
-
42
-This is a simple php engine to help us render template.
43
-
44
-``` php
45
-<h1><?php echo $this->escape($title); ?></h1>
46
-```
47
-
48
-### Include Sub Template
49
-
50
-Use `load()` to load other template file as a block. The first argument is file path, the second argument is new data
51
-to merge with original data.
52
-
53
-``` php
54
-echo $this->load('sub.template', array('bar' => 'baz'));
55
-```
56
-
57
-Example to load `foo/article.php`:
58
-
59
-``` php
60
-<h1><?php echo $this->escape($title); ?></h1>
61
-
62
-<?php foreach ($data->articles as $article): ?>
63
-    <?php echo $this->load('foo.article', array('bar' => 'baz')); ?>
64
-<?php endforeach; ?>
65
-```
66
-
67
-### Extends Parent Template
68
-
69
-In Windwalker Renderer, there is a powerful function like Twig or Blade, we provide `extend()` method to extends
70
-parent template. (`extends` in php is a reserved string, so we can only use `extend`)
71
-
72
-For example, this is the parent `_global/html.php` template:
73
-
74
-``` php
75
-<Doctype html>
76
-<html>
77
-<head>
78
-    <title><?php $this->block('title');?>Home<?php $this->endblock(); ?></title>
79
-</head>
80
-<body>
81
-    <div class="container">
82
-    <?php $this->block('body');?>
83
-        <h2>Home page</h2>
84
-    <?php $this->endblock(); ?>
85
-    </div>
86
-</body>
87
-</html>
88
-```
89
-
90
-And we can extends it in our View:
91
-
92
-``` php
93
-<?php
94
-$this->extend('_global.html');
95
-?>
96
-
97
-<?php $this->block('title');?>Article<?php $this->endblock(); ?>
98
-
99
-<?php $this->block('body');?>
100
-    <article>
101
-        <h2>Article</h2>
102
-        <p>FOO</p>
103
-    </article>
104
-<?php $this->endblock(); ?>
105
-```
106
-
107
-The result will be:
108
-
109
-``` html
110
-<Doctype html>
111
-<html>
112
-<head>
113
-    <title>Article</title>
114
-</head>
115
-<body>
116
-    <div class="container">
117
-        <article>
118
-            <h2>Article</h2>
119
-            <p>FOO</p>
120
-        </article>
121
-    </div>
122
-</body>
123
-</html>
124
-```
125
-
126
-### Show Parent
127
-
128
-We can echo parent data in a block:
129
-
130
-``` php
131
-<?php $this->block('body');?>
132
-    <?php echo $this->parent(); ?>
133
-    <article>
134
-        <h2>Article</h2>
135
-        <p>FOO</p>
136
-    </article>
137
-<?php $this->endblock(); ?>
138
-```
139
-
140
-Result:
141
-
142
-``` html
143
-<h2>Home page</h2>
144
-<article>
145
-    <h2>Article</h2>
146
-    <p>FOO</p>
147
-</article>
148
-```
149
-
150
-## Add More Paths to Search
151
-
152
-We create 3 paths by `SplPriorityQueue`, that make theme path is priority to others, so we can override view templates
153
- by theme, and view can also override system template.
154
-
155
-
156
-``` php
157
-$paths = new \SplPriorityQueue;
158
-
159
-$paths->insert('path/to/system', 100);
160
-$paths->insert('path/to/view', 200);
161
-$paths->insert('path/to/theme', 300);
162
-
163
-$renderer = new PhpRenderer($paths);
164
-
165
-$renderer->render('foo', $data);
166
-```
167
-
168
-## Twig Renderer
169
-
170
-[Twig](http://twig.sensiolabs.org/) Renderer help us render files by twig engine.
171
-
172
-``` php
173
-use Windwalker\Renderer\TwigRenderer;
174
-
175
-$renderer = new TwigRenderer($paths);
176
-
177
-$renderer->render('foo', $data);
178
-```
179
-
180
-### Set custom Twig instance or Loader.
181
-
182
-``` php
183
-$renderer->setEngine(new \Twig_Environment);
184
-$renderer->setLoader(new MyTwigLoader);
185
-```
186
-
187
-### Add Twig Extensions
188
-
189
-``` php
190
-$renderer->addExtension(new MyTwigExtension);
191
-```
192
-
193
-### Debug Mode
194
-
195
-Set debug config when construct:
196
-
197
-``` php
198
-$renderer = new TwigRenderer($paths, array('debug' => true));
199
-```
200
-
201
-## Blade Renderer
202
-
203
-Blade is a powerful php template engine which created by Laravel. We integrate it in our Renderer that eveyone can use it without Laravel.
204
-
205
-Add this line to your composer require block and run `composer update`:
206
-
207
-``` json
208
-"illuminate/view" : "4.*"
209
-```
210
-
211
-Create Blade Renderer:
212
-
213
-``` php
214
-use Windwalker\Renderer\BladeRenderer;
215
-
216
-$renderer = new BladeRenderer($paths, array('cache_path' => __DIR__ . '/cache'));
217
-
218
-$renderer->render('foo', $data);
219
-```
220
-
221
-The file name must suffix with `.blade.php`.
222
-
223
-### Add Custom Compilers
224
-
225
-``` php
226
-$renderer = new BladeRenderer($paths, array('cache_path' => __DIR__ . '/cache'));
227
-
228
-$renderer->addCustomCompiler('datetime', function($expression)
229
-{
230
-    return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
231
-});
232
-```
233
-
234
-More about Blade engine please see [Laravel Document](http://laravel.com/docs/4.2/templates#blade-templating).
235
-
236
-## Edge Renderer
237
-
238
-Edge is a Blade compatible template engine which created to support Windwalker itself.
239
-
240
-``` php
241
-$renderer = new EdgeRenderer;
242
-
243
-// Ad custom extensions
244
-$renderer->addExtension(new MyEdgeExtension);
245
-
246
-echo $renderer->render('layout.main', $data);
247
-```
248
-
249
-Cache files:
250
-
251
-``` php
252
-$renderer = new EdgeRenderer($paths, array('cache_path' => __DIR__ . '/cache'));
253
-echo $renderer->render('layout.main', $data);
254
-```
255
-
256
-See [Windwalker Edge](https://github.com/ventoviro/windwalker-edge)
257
-
258
-## Mustache Renderer
259
-
260
-We also provide [Mustache](http://mustache.github.io/) Renderer.
261
-
262
-Add this line to your composer require block and run `composer update`:
263
-
264
-``` json
265
-"mustache/mustache" : "2.*"
266
-```
267
-
268
-Create Mustache Renderer:
269
-
270
-``` php
271
-use Windwalker\Renderer\MustacheRenderer;
272
-
273
-class Chris
274
-{
275
-	public $name  = "Chris";
276
-	public $value = 10000;
277
-
278
-	public function taxed_value()
279
-	{
280
-		return $this->value - ($this->value * 0.4);
281
-	}
282
-
283
-	public $in_ca = true;
284
-}
285
-
286
-$renderer = new MustacheRenderer($paths);
287
-
288
-$renderer->render('foo', new Chris)
289
-```
290
-
291
-The file is `foo.mustache`:
292
-
293
-``` mustache
294
-Hello {{name}}
295
-You have just won ${{value}}!
296
-{{#in_ca}}
297
-Well, ${{taxed_value}}, after taxes.
298
-{{/in_ca}}
299
-```
300
-
301
-Abd the output:
302
-
303
-``` html
304
-Hello Chris
305
-You have just won $10000!
306
-Well, $6000, after taxes.
307
-```
308
-
309
-### Loader
310
-
311
-``` php
312
-$renderer->setLoader(new \Mustache_Loader_FilesystemLoader($path, $options));
313
-```
314
-
315
-### Config
316
-
317
-We can change the file extension name and many other configs, please see [Mustache PHP Document](https://github.com/bobthecow/mustache.php/wiki).
318
-
319
-## Plates Renderer
320
-
321
-``` php
322
-use Windwalker\Renderer\PlatesRenderer;
323
-
324
-$renderer = new PlatesRenderer;
325
-
326
-echo $renderer->render('flower.sakura', array('foo' => 'bar'));
327
-```
328
-
329
-See: [Plates](http://platesphp.com/)
330
-
331
-## Use Cases
332
-
333
-Renderer has many use cases, one of often usage is that integrating it with view object as engine and loader.
334
-
335
-``` php
336
-class View
337
-{
338
-    public function __construct($data = array(), RendererInterface $renderer = null)
339
-    {
340
-        $this->data = $data;
341
-        $this->renderer = $renderer ? : new PhpRenderer;
342
-    }
343
-
344
-    public function render($layout)
345
-    {
346
-        // Do some stuff...
347
-
348
-        return $this->renderer->render($layout, $this->data);
349
-    }
350
-}
351
-```
352
-
353
-Or be a layout widget:
354
-
355
-``` php
356
-class Widget extends PhpRenderer
357
-{
358
-    public function __construct($config = array())
359
-    {
360
-        $paths = array(
361
-            'path/of/theme/widget',
362
-            'path/of/system/widget'
363
-        );
364
-
365
-        parent::__construct($paths, $config);
366
-    }
367
-}
368
-
369
-// Call this class everywhere
370
-echo (new Widget)->render('foo', array('bar' => 'baz'));
371
-```
Browse code

initial commit

Emmanuel ROY authored on 09/08/2019 08:39:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,371 @@
1
+# Windwalker Renderer
2
+
3
+Windwalker Renderer is a simple template engine loader to load file for engines to render.
4
+
5
+## Installation via Composer
6
+
7
+Add this to the require block in your `composer.json`.
8
+
9
+``` json
10
+{
11
+    "require": {
12
+        "windwalker/renderer": "~3.0"
13
+    }
14
+}
15
+```
16
+
17
+## Support Engines
18
+
19
+- [PHP](#getting-started)
20
+- [Twig](#twig-renderer)
21
+- [Blade](#blade-renderer)
22
+- [Edge](#edge-renderer) (A Blade compitable engine without dependencies)
23
+- [Mustache](#mustache-renderer)
24
+- [Plates](#plates-renderer)
25
+
26
+## Getting Started
27
+
28
+``` php
29
+use Windwalker\Renderer\PhpRenderer;
30
+
31
+$config = array();
32
+
33
+$renderer = new PhpRenderer(__DIR__ . '/file/path', $config);
34
+
35
+$data = array('title' => 'foo');
36
+
37
+echo $renderer->render('template', $data);
38
+```
39
+
40
+### In `template.php`
41
+
42
+This is a simple php engine to help us render template.
43
+
44
+``` php
45
+<h1><?php echo $this->escape($title); ?></h1>
46
+```
47
+
48
+### Include Sub Template
49
+
50
+Use `load()` to load other template file as a block. The first argument is file path, the second argument is new data
51
+to merge with original data.
52
+
53
+``` php
54
+echo $this->load('sub.template', array('bar' => 'baz'));
55
+```
56
+
57
+Example to load `foo/article.php`:
58
+
59
+``` php
60
+<h1><?php echo $this->escape($title); ?></h1>
61
+
62
+<?php foreach ($data->articles as $article): ?>
63
+    <?php echo $this->load('foo.article', array('bar' => 'baz')); ?>
64
+<?php endforeach; ?>
65
+```
66
+
67
+### Extends Parent Template
68
+
69
+In Windwalker Renderer, there is a powerful function like Twig or Blade, we provide `extend()` method to extends
70
+parent template. (`extends` in php is a reserved string, so we can only use `extend`)
71
+
72
+For example, this is the parent `_global/html.php` template:
73
+
74
+``` php
75
+<Doctype html>
76
+<html>
77
+<head>
78
+    <title><?php $this->block('title');?>Home<?php $this->endblock(); ?></title>
79
+</head>
80
+<body>
81
+    <div class="container">
82
+    <?php $this->block('body');?>
83
+        <h2>Home page</h2>
84
+    <?php $this->endblock(); ?>
85
+    </div>
86
+</body>
87
+</html>
88
+```
89
+
90
+And we can extends it in our View:
91
+
92
+``` php
93
+<?php
94
+$this->extend('_global.html');
95
+?>
96
+
97
+<?php $this->block('title');?>Article<?php $this->endblock(); ?>
98
+
99
+<?php $this->block('body');?>
100
+    <article>
101
+        <h2>Article</h2>
102
+        <p>FOO</p>
103
+    </article>
104
+<?php $this->endblock(); ?>
105
+```
106
+
107
+The result will be:
108
+
109
+``` html
110
+<Doctype html>
111
+<html>
112
+<head>
113
+    <title>Article</title>
114
+</head>
115
+<body>
116
+    <div class="container">
117
+        <article>
118
+            <h2>Article</h2>
119
+            <p>FOO</p>
120
+        </article>
121
+    </div>
122
+</body>
123
+</html>
124
+```
125
+
126
+### Show Parent
127
+
128
+We can echo parent data in a block:
129
+
130
+``` php
131
+<?php $this->block('body');?>
132
+    <?php echo $this->parent(); ?>
133
+    <article>
134
+        <h2>Article</h2>
135
+        <p>FOO</p>
136
+    </article>
137
+<?php $this->endblock(); ?>
138
+```
139
+
140
+Result:
141
+
142
+``` html
143
+<h2>Home page</h2>
144
+<article>
145
+    <h2>Article</h2>
146
+    <p>FOO</p>
147
+</article>
148
+```
149
+
150
+## Add More Paths to Search
151
+
152
+We create 3 paths by `SplPriorityQueue`, that make theme path is priority to others, so we can override view templates
153
+ by theme, and view can also override system template.
154
+
155
+
156
+``` php
157
+$paths = new \SplPriorityQueue;
158
+
159
+$paths->insert('path/to/system', 100);
160
+$paths->insert('path/to/view', 200);
161
+$paths->insert('path/to/theme', 300);
162
+
163
+$renderer = new PhpRenderer($paths);
164
+
165
+$renderer->render('foo', $data);
166
+```
167
+
168
+## Twig Renderer
169
+
170
+[Twig](http://twig.sensiolabs.org/) Renderer help us render files by twig engine.
171
+
172
+``` php
173
+use Windwalker\Renderer\TwigRenderer;
174
+
175
+$renderer = new TwigRenderer($paths);
176
+
177
+$renderer->render('foo', $data);
178
+```
179
+
180
+### Set custom Twig instance or Loader.
181
+
182
+``` php
183
+$renderer->setEngine(new \Twig_Environment);
184
+$renderer->setLoader(new MyTwigLoader);
185
+```
186
+
187
+### Add Twig Extensions
188
+
189
+``` php
190
+$renderer->addExtension(new MyTwigExtension);
191
+```
192
+
193
+### Debug Mode
194
+
195
+Set debug config when construct:
196
+
197
+``` php
198
+$renderer = new TwigRenderer($paths, array('debug' => true));
199
+```
200
+
201
+## Blade Renderer
202
+
203
+Blade is a powerful php template engine which created by Laravel. We integrate it in our Renderer that eveyone can use it without Laravel.
204
+
205
+Add this line to your composer require block and run `composer update`:
206
+
207
+``` json
208
+"illuminate/view" : "4.*"
209
+```
210
+
211
+Create Blade Renderer:
212
+
213
+``` php
214
+use Windwalker\Renderer\BladeRenderer;
215
+
216
+$renderer = new BladeRenderer($paths, array('cache_path' => __DIR__ . '/cache'));
217
+
218
+$renderer->render('foo', $data);
219
+```
220
+
221
+The file name must suffix with `.blade.php`.
222
+
223
+### Add Custom Compilers
224
+
225
+``` php
226
+$renderer = new BladeRenderer($paths, array('cache_path' => __DIR__ . '/cache'));
227
+
228
+$renderer->addCustomCompiler('datetime', function($expression)
229
+{
230
+    return "<?php echo with{$expression}->format('m/d/Y H:i'); ?>";
231
+});
232
+```
233
+
234
+More about Blade engine please see [Laravel Document](http://laravel.com/docs/4.2/templates#blade-templating).
235
+
236
+## Edge Renderer
237
+
238
+Edge is a Blade compatible template engine which created to support Windwalker itself.
239
+
240
+``` php
241
+$renderer = new EdgeRenderer;
242
+
243
+// Ad custom extensions
244
+$renderer->addExtension(new MyEdgeExtension);
245
+
246
+echo $renderer->render('layout.main', $data);
247
+```
248
+
249
+Cache files:
250
+
251
+``` php
252
+$renderer = new EdgeRenderer($paths, array('cache_path' => __DIR__ . '/cache'));
253
+echo $renderer->render('layout.main', $data);
254
+```
255
+
256
+See [Windwalker Edge](https://github.com/ventoviro/windwalker-edge)
257
+
258
+## Mustache Renderer
259
+
260
+We also provide [Mustache](http://mustache.github.io/) Renderer.
261
+
262
+Add this line to your composer require block and run `composer update`:
263
+
264
+``` json
265
+"mustache/mustache" : "2.*"
266
+```
267
+
268
+Create Mustache Renderer:
269
+
270
+``` php
271
+use Windwalker\Renderer\MustacheRenderer;
272
+
273
+class Chris
274
+{
275
+	public $name  = "Chris";
276
+	public $value = 10000;
277
+
278
+	public function taxed_value()
279
+	{
280
+		return $this->value - ($this->value * 0.4);
281
+	}
282
+
283
+	public $in_ca = true;
284
+}
285
+
286
+$renderer = new MustacheRenderer($paths);
287
+
288
+$renderer->render('foo', new Chris)
289
+```
290
+
291
+The file is `foo.mustache`:
292
+
293
+``` mustache
294
+Hello {{name}}
295
+You have just won ${{value}}!
296
+{{#in_ca}}
297
+Well, ${{taxed_value}}, after taxes.
298
+{{/in_ca}}
299
+```
300
+
301
+Abd the output:
302
+
303
+``` html
304
+Hello Chris
305
+You have just won $10000!
306
+Well, $6000, after taxes.
307
+```
308
+
309
+### Loader
310
+
311
+``` php
312
+$renderer->setLoader(new \Mustache_Loader_FilesystemLoader($path, $options));
313
+```
314
+
315
+### Config
316
+
317
+We can change the file extension name and many other configs, please see [Mustache PHP Document](https://github.com/bobthecow/mustache.php/wiki).
318
+
319
+## Plates Renderer
320
+
321
+``` php
322
+use Windwalker\Renderer\PlatesRenderer;
323
+
324
+$renderer = new PlatesRenderer;
325
+
326
+echo $renderer->render('flower.sakura', array('foo' => 'bar'));
327
+```
328
+
329
+See: [Plates](http://platesphp.com/)
330
+
331
+## Use Cases
332
+
333
+Renderer has many use cases, one of often usage is that integrating it with view object as engine and loader.
334
+
335
+``` php
336
+class View
337
+{
338
+    public function __construct($data = array(), RendererInterface $renderer = null)
339
+    {
340
+        $this->data = $data;
341
+        $this->renderer = $renderer ? : new PhpRenderer;
342
+    }
343
+
344
+    public function render($layout)
345
+    {
346
+        // Do some stuff...
347
+
348
+        return $this->renderer->render($layout, $this->data);
349
+    }
350
+}
351
+```
352
+
353
+Or be a layout widget:
354
+
355
+``` php
356
+class Widget extends PhpRenderer
357
+{
358
+    public function __construct($config = array())
359
+    {
360
+        $paths = array(
361
+            'path/of/theme/widget',
362
+            'path/of/system/widget'
363
+        );
364
+
365
+        parent::__construct($paths, $config);
366
+    }
367
+}
368
+
369
+// Call this class everywhere
370
+echo (new Widget)->render('foo', array('bar' => 'baz'));
371
+```