Last active
          November 13, 2022 01:11 
        
      - 
      
- 
        Save elazar/2d6f23c5384f60e32f0ff024a330c145 to your computer and use it in GitHub Desktop. 
Revisions
- 
        elazar revised this gist Nov 13, 2022 . 1 changed file with 31 additions and 25 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,25 @@ function array_is_list(array $array): bool } } $data = [ [ 'description' => 'Foo', 'price' => 1.99, ], [ 'description' => 'Bar', 'price' => 2.88, ], [ 'description' => 'Baz', 'price' => 3.77, ], [ 'description' => 'Bay', 'price' => 4.66, ], ]; class Element implements \Stringable { private array $attributes = []; @@ -44,40 +63,27 @@ public function __toString(): string } } $elements = [ 'table', 'th', 'tr', 'td', ]; foreach ($elements as $element) { $$element = new Element($element); } echo $table(id: 'table-id')( $tr( $th('Description'), $th(class: 'text-right')('Price'), ), ...array_map(fn(array $row) => $tr( $td($row['description']), $td(number_format($row['price'], 2)), ), $data, ), ); 
- 
        elazar revised this gist Nov 13, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ public function __toString(): string $tr = $element('tr'); $td = $element('td'); echo $table(id: 'table-id')( $tr( $th('Description'), $th(class: 'text-right')('Price'), 
- 
        elazar created this gist Feb 28, 2022 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ <?php if (!function_exists('array_is_list')) { function array_is_list(array $array): bool { return array_keys($array) === range(0, count($array) - 1); } } class Element implements \Stringable { private array $attributes = []; private ?array $children = null; public function __construct( private string $name, ) { } public function __invoke(...$args): static|string { if (array_is_list($args)) { $this->children = $args; return $this->__toString(); } $this->attributes = $args; return $this; } public function __toString(): string { $rendered = '<' . $this->name; foreach ($this->attributes as $attribute => $value) { $rendered .= ' ' . $attribute; if ($value !== true) { $rendered .= '="' . htmlentities($value) . '"'; } } $rendered .= '>'; if ($this->children) { $rendered .= implode('', $this->children) . '</' . $this->name . '>'; } return $rendered; } } $element = fn(string $name) => new Element($name); $table = $element('table'); $th = $element('th'); $tr = $element('tr'); $td = $element('td'); echo $table(id: "table-id")( $tr( $th('Description'), $th(class: 'text-right')('Price'), ), ...array_map( fn(array $row) => $tr( $td($row['description']), $td(number_format($row['price'], 2)), ), [ [ 'description' => 'Foo', 'price' => 1.99, ], [ 'description' => 'Bar', 'price' => 2.88, ], [ 'description' => 'Baz', 'price' => 3.77, ], [ 'description' => 'Bay', 'price' => 4.66, ], ], ), );