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
+75-9
Original file line number
Diff line number
Diff line change
@@ -412,6 +412,20 @@ Note that the `<template webc:type>` node is compiled away. If you’d like to k
412
412
413
413
We do provide two built-in transforms in WebC: JavaScript Render Functions (`webc:type="render"`) and CSS scoping (`webc:scoped`). Those are covered in separate sections. You _can_ override these with the `setTransform` API but it is generally recommended to add your own named transform!
414
414
415
+
### Conditionals
416
+
417
+
_(WebC v0.7.1+)_
418
+
419
+
Use `webc:if` to conditionally render elements. Accepts arbitrary JavaScript (and is async-friendly). Similar to dynamic attributes, this also has access to component attributes and properties.
420
+
421
+
```html
422
+
<divwebc:if="true">This will render</div>
423
+
<divwebc:if="false">This will not render</div>
424
+
<divwebc:if="myAsyncHelper()">If the helper promise resolves to a truthy value, this will render</div>
425
+
```
426
+
427
+
For more complex conditionals, `webc:type="js"`_(WebC v0.7.1+)_ is recommended (read more below).
428
+
415
429
### Attributes
416
430
417
431
Consider this example:
@@ -473,7 +487,16 @@ Make any attribute into a prop by prefixing it with `@`. Props are “private”
473
487
474
488
### JavaScript Render Functions
475
489
476
-
You can also transform individual element content using `webc:type`. We provide one built-in type, `render` for JavaScript render functions. These are async friendly (e.g. `async function()`):
490
+
You can also transform individual element content using `webc:type`. There are three built-in types:
491
+
492
+
*`webc:type="js"` which supercedes `webc:type="render"`
493
+
*`webc:type="css:scoped"` (internal for `webc:scoped`—overridable!)
494
+
495
+
JavaScript Render Functions are async friendly (e.g. `async function()`):
496
+
497
+
#### `webc:type="js"`_(WebC v0.7.1+)_
498
+
499
+
Run any arbitrary server JavaScript in WebC. Outputs the result of the very last statement executed in the script. Async-friendly (return a promise and we’ll resolve it).
477
500
478
501
`page.webc`:
479
502
@@ -483,18 +506,32 @@ You can also transform individual element content using `webc:type`. We provide
483
506
484
507
`components/img.webc`:
485
508
509
+
```html
510
+
<scriptwebc:type="js"webc:is="template">
511
+
if(!alt) {
512
+
thrownewError("oh no you didn’t");
513
+
}
514
+
`<img src="${src}" alt="${alt}">`;
515
+
</script>
516
+
```
517
+
518
+
<details>
519
+
<summary>Expand to see this example with <code>webc:type="render"</code></summary>
520
+
486
521
```html
487
522
<scriptwebc:type="render"webc:is="template">
488
-
function() {
489
-
if(!this.alt) {
490
-
thrownewError("oh no you didn’t");
523
+
function() {
524
+
if(!this.alt) {
525
+
thrownewError("oh no you didn’t");
491
526
}
492
527
// Free idea: use the Eleventy Image plugin to return optimized markup
<summary>Expand to see this example with <code>webc:type="render"</code></summary>
554
+
510
555
```html
511
556
<scriptwebc:type="render"webc:is="style">
512
557
function() {
@@ -516,11 +561,32 @@ function() {
516
561
<slot></slot>
517
562
```
518
563
519
-
(Yes you can use `<script webc:type="render" webc:scoped>` here too).
564
+
</details>
565
+
566
+
(Yes you can use `<script webc:type="js" webc:scoped>` too).
567
+
568
+
A few more examples of conditionals:
569
+
570
+
```html
571
+
<scriptwebc:type="js">
572
+
alt ?`<img src="${src}" alt="${alt}">`:`<a href="${src}">Your image didn’t have an alt so you get this link instead.</a>`
573
+
</script>
574
+
```
520
575
521
576
Note that you have access to the component attributes and properties in the render function (which is covered in another section!).
522
577
523
-
#### Setting HTML
578
+
```html
579
+
<scriptwebc:type="js">
580
+
if(alt) {
581
+
`<img src="${src}" alt="${alt}">`
582
+
} else {
583
+
`<a href="${src}">Your image didn’t have an alt so you get this link instead.</a>`
584
+
}
585
+
</script>
586
+
```
587
+
588
+
589
+
### Setting HTML
524
590
525
591
We provide a special `@html` property to override any tag content with custom JavaScript.
526
592
@@ -537,7 +603,7 @@ We provide a special `@html` property to override any tag content with custom Ja
537
603
* Using `webc:raw` will prevent reprocessing the result as WebC. (v0.6.0+)
538
604
* Use `@raw` as an alias for `webc:raw @html` . (v0.7.1+)
539
605
540
-
####Setting Text
606
+
### Setting Text
541
607
542
608
We provide a special `@text` property to override any tag content with custom JavaScript. The entire value returned here will be escaped!
543
609
@@ -553,7 +619,7 @@ We provide a special `@text` property to override any tag content with custom Ja
553
619
<p@text="dataProperty"webc:nokeep></p>
554
620
```
555
621
556
-
####Helper Functions
622
+
### Helper Functions
557
623
558
624
If you want to add custom JavaScript functions for use in render functions, `@html`, or dynamic attributes you can use the `setHelper` method.
559
625
@@ -579,7 +645,7 @@ function() {
579
645
580
646
### Raw Content (no WebC processing)
581
647
582
-
Opt out of WebC template processing using `webc:raw`. This works well with `<template>` content.
648
+
Opt out of WebC template processing using `webc:raw`. This works well with `<template>` content. See also the special `@raw` content property _(WebC v0.7.1+)_
0 commit comments