Skip to content

Instantly share code, notes, and snippets.

@elazar
Last active November 13, 2022 01:11
Show Gist options
  • Save elazar/2d6f23c5384f60e32f0ff024a330c145 to your computer and use it in GitHub Desktop.
Save elazar/2d6f23c5384f60e32f0ff024a330c145 to your computer and use it in GitHub Desktop.

Revisions

  1. elazar revised this gist Nov 13, 2022. 1 changed file with 31 additions and 25 deletions.
    56 changes: 31 additions & 25 deletions html-in-php.php
    Original 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
    }
    }

    $element = fn(string $name) => new Element($name);
    $elements = [
    'table',
    'th',
    'tr',
    'td',
    ];

    $table = $element('table');
    $th = $element('th');
    $tr = $element('tr');
    $td = $element('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(
    ...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,
    ],
    ],
    $data,
    ),
    );
  2. elazar revised this gist Nov 13, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion html-in-php.php
    Original 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")(
    echo $table(id: 'table-id')(
    $tr(
    $th('Description'),
    $th(class: 'text-right')('Price'),
  3. elazar created this gist Feb 28, 2022.
    83 changes: 83 additions & 0 deletions html-in-php.php
    Original 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,
    ],
    ],
    ),
    );