|
1 | 1 | # Tags
|
| 2 | + |
| 3 | +All non-depreacted HTML tags are defined as instance methods on `Phlex::HTML`. They take keyword arguments which get turned into HTML attributes, and blocks which become content for the tag. |
| 4 | + |
| 5 | +## Attributes |
| 6 | +Attributes are normally symbol keys, which get dasherized. If you need a special attribute that has underscores in it, you can use a string key instead. |
| 7 | + |
| 8 | +::: code-group |
| 9 | +```ruby |
| 10 | +div(data_controller: "hello", "_special": "value") do |
| 11 | + "Hello!" |
| 12 | +end |
| 13 | +``` |
| 14 | +```html |
| 15 | +<div data-controller="hello" _special="value"> |
| 16 | + Hello! |
| 17 | +</div> |
| 18 | +``` |
| 19 | +::: |
| 20 | + |
| 21 | +Alternatively, if you have multiple nested attributes (like several `data-*` attributes) you can use a hash: |
| 22 | + |
| 23 | +::: code-group |
| 24 | +```ruby |
| 25 | +div(data: { controller: "hello", action: "click->hello#show" }) do |
| 26 | + "Hello!" |
| 27 | +end |
| 28 | +``` |
| 29 | +```html |
| 30 | +<div data-controller="hello" data-action="click->hello#show"> |
| 31 | + Hello! |
| 32 | +</div> |
| 33 | +``` |
| 34 | +::: |
| 35 | + |
| 36 | +The `class` and `style` attributes have special handling. If you use a hash value with the `class` key, it will work similarly to the Rails' `class_names` helper when passed a hash: |
| 37 | + |
| 38 | +::: code-group |
| 39 | +```ruby |
| 40 | +is_active = true |
| 41 | +is_disabled = false |
| 42 | + |
| 43 | +div(class: { active: is_active, disabled: is_disabled }) do |
| 44 | + "Hello!" |
| 45 | +end |
| 46 | +``` |
| 47 | +```html |
| 48 | +<div class="active"> |
| 49 | + Hello! |
| 50 | +</div> |
| 51 | +``` |
| 52 | +::: |
| 53 | + |
| 54 | +If you use a hash value with the `style` key, it will be converted to a CSS string: |
| 55 | + |
| 56 | +::: code-group |
| 57 | +```ruby |
| 58 | +h1(style: { color: "red", font_size: "16px" }) do |
| 59 | + "Hello!" |
| 60 | +end |
| 61 | +``` |
| 62 | +```html |
| 63 | +<h1 style="color: red; font-size: 16px;"> |
| 64 | + Hello! |
| 65 | +</h1> |
| 66 | +``` |
| 67 | +::: |
| 68 | + |
| 69 | +## Content |
| 70 | + |
| 71 | +Content is always passed as a block to the tag method. The block content works differently depending on whether or not other tag methods are called inside the block. |
| 72 | + |
| 73 | +If there are no other tag methods called inside the block, then the return value of the block is used as the content |
| 74 | + |
| 75 | +::: code-group |
| 76 | +```ruby |
| 77 | +div do |
| 78 | + "Hello!" |
| 79 | +end |
| 80 | +``` |
| 81 | +```html |
| 82 | +<div> |
| 83 | + Hello! |
| 84 | +</div> |
| 85 | +``` |
| 86 | +::: |
| 87 | + |
| 88 | +If there are other tag methods called inside the block, then the return value of the block is ignored. Instead, if you need to pass string content outside of a nested tag, you can use the `plain` method. |
| 89 | + |
| 90 | +::: code-group |
| 91 | +```ruby |
| 92 | +p do |
| 93 | + strong { "Hello" } |
| 94 | + plain " World!" |
| 95 | +end |
| 96 | +``` |
| 97 | +```html |
| 98 | +<p> |
| 99 | + <strong>Hello</strong> World! |
| 100 | +</p> |
| 101 | +``` |
| 102 | +::: |
| 103 | + |
| 104 | +If we wrote that without the `plain` method, we would be missing the ` World!` part. |
| 105 | + |
| 106 | +::: code-group |
| 107 | +```ruby |
| 108 | +p do |
| 109 | + strong { "Hello" } |
| 110 | + " World!" |
| 111 | +end |
| 112 | +``` |
| 113 | +```html |
| 114 | +<p> |
| 115 | + <strong>Hello</strong> |
| 116 | +</p> |
| 117 | +``` |
| 118 | +::: |
| 119 | + |
| 120 | +That is because the `p` tag's block has another tag inside of it, so it ignores the return value of the block. |
0 commit comments