You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: readme.md
+258
Original file line number
Diff line number
Diff line change
@@ -241,12 +241,270 @@ With namespaces you can define a top level root path to your components like sho
241
241
242
242
### Slots
243
243
244
+
Your components can inject code in specific slots you define, and then you can fill this content when you use the component.
245
+
Find below a simple example.
246
+
247
+
Create the component:
248
+
249
+
```html
250
+
<!-- src/modal.html -->
251
+
<divclass="modal">
252
+
<divclass="modal-header">
253
+
<slot:header></slot:header>
254
+
</div>
255
+
<divclass="modal-body">
256
+
<slot:body></slot:body>
257
+
</div>
258
+
<divclass="modal-footer">
259
+
<slot:footer></slot:footer>
260
+
</div>
261
+
</div>
262
+
```
263
+
264
+
Use the component:
265
+
266
+
```html
267
+
<!-- src/index.html -->
268
+
<x-modal>
269
+
<fill:header>Header content</fill:header>
270
+
<fill:body>Body content</fill:body>
271
+
<fill:footer>Footer content</fill:footer>
272
+
</x-modal>
273
+
```
274
+
275
+
Result:
276
+
277
+
```html
278
+
<!-- dist/index.html -->
279
+
<divclass="modal">
280
+
<divclass="modal-header">
281
+
Header content
282
+
</div>
283
+
<divclass="modal-body">
284
+
Body content
285
+
</div>
286
+
<divclass="modal-footer">
287
+
Footer content
288
+
</div>
289
+
</div>
290
+
```
291
+
292
+
By default the content is replaced, but you can also prepend or append the content, or keep the default content by not filling the slot.
293
+
294
+
Add some default content in the component:
295
+
296
+
```html
297
+
<!-- src/modal.html -->
298
+
<divclass="modal">
299
+
<divclass="modal-header">
300
+
<slot:header>Default header</slot:header>
301
+
</div>
302
+
<divclass="modal-body">
303
+
<slot:body>content</slot:body>
304
+
</div>
305
+
<divclass="modal-footer">
306
+
<slot:footer>Footer</slot:footer>
307
+
</div>
308
+
</div>
309
+
```
310
+
311
+
```html
312
+
<!-- src/index.html -->
313
+
<x-modal>
314
+
<fill:bodyprepend>Prepend body</fill:body>
315
+
<fill:footerappend>content</fill:footer>
316
+
</x-modal>
317
+
```
318
+
319
+
Result:
320
+
321
+
```html
322
+
<!-- dist/index.html -->
323
+
<divclass="modal">
324
+
<divclass="modal-header">
325
+
Default header
326
+
</div>
327
+
<divclass="modal-body">
328
+
Prepend body content
329
+
</div>
330
+
<divclass="modal-footer">
331
+
Footer content
332
+
</div>
333
+
</div>
334
+
```
335
+
244
336
### Stacks
245
337
338
+
You can push content to named stacks which can be rendered somewhere else in another place. This can be particularly useful for specifying any JavaScript or CSS required by your components.
339
+
340
+
First of all define a `<stack>` anywhere in your code, for example:
341
+
342
+
```html
343
+
<!-- src/index.html -->
344
+
<html>
345
+
<head>
346
+
<stackname="styles"></stack>
347
+
</head>
348
+
<body>
349
+
350
+
<x-modal>
351
+
<fill:header>Header content</fill:header>
352
+
<fill:body>Body content</fill:body>
353
+
<fill:footer>Footer content</fill:footer>
354
+
</x-modal>
355
+
356
+
<stackname="scripts"></stack>
357
+
</body>
358
+
</html>
359
+
```
360
+
361
+
Then in modal components, or any other child components, you can push content to this stack.
The `once` attribute allows you to push content only once per rendering cycle. For example, if you are rendering a given component within a loop, you may wish to only push the JavaScript and CSS the first time the component is rendered.
412
+
413
+
Example.
414
+
415
+
```html
416
+
<!-- src/modal.html -->
417
+
<divclass="modal">
418
+
<!-- ... -->
419
+
</div>
420
+
421
+
<!-- The push content will be pushed only once in the stack -->
You can add custom rules how attributes are parsed, as behind the scene it's used [posthtml-attrs-parser](https://github.com/posthtml/posthtml-attrs-parser) plugin.
0 commit comments