In some cases, table data includes image paths or URLs, such as user profile pictures, product images, or icons. Instead of displaying raw text, you can format these values as images using middleware.
Create an ImageColumnMiddleware
use Ucscode\HtmlComponent\TableGenerator\Abstraction\AbstractMiddleware;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Td;
use Ucscode\HtmlComponent\TableGenerator\Contracts\CellInterface;
use Ucscode\HtmlComponent\TableGenerator\Contracts\TableSegmentInterface;
use Ucscode\HtmlComponent\TableGenerator\Component\Section\Tr;
use Ucscode\UssElement\Node\ElementNode;
class ImageColumnMiddleware extends AbstractMiddleware
{
public function process(Table $table): void
{
$this->iterateCellsIn($table, function (CellInterface $cell) {
// Check if the column should contain an image
if ($cell->getMeta()->get('columnName') === 'profile_picture') {
// Get image URL from cell data
$imageUrl = $cell->getData();
// Create an img element
$image = new ElementNode('img', [
'src' => $imageUrl,
'alt' => 'Profile Picture',
'width' => '100',
'height' => '10',
]);
// Set image as the new cell content
$cell->setData($image);
}
});
}
}
Use the ImageColumnMiddleware
in the TableGenerator
$tableGenerator = new TableGenerator($adapter, new ImageColumnMiddleware());
// Render the table
echo $tableGenerator->render();
Expected Output
Name | Profile Picture |
---|---|
John Doe | |
Jane Doe |
This middleware ensures that image URLs are converted into proper <img>
elements inside the table.