PHP code example of icmbio / json2html

1. Go to this page and download the library: Download icmbio/json2html library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

icmbio / json2html example snippets




use Icmbio\Json2html\RenderTable;

$data = [
    "name" => "json2html", 
    "description" => "Converts JSON to HTML"
];

$table = new RenderTable($data);
echo $table->render();

$data = [
    "Monitor" => [
        ["Nome" => "Luiz Loureiro"],
        ["Nome" => "Michele Rocha Silva"]  
    ]
];

$table = new RenderTable($data);
echo $table->render();

use Icmbio\Json2html\{RenderTable, TableOrientation};

$data = [
    "name" => "json2html",
    "description" => "Converts JSON to HTML"  
];

$table = (new RenderTable($data))
    ->config(['orientation' => TableOrientation::HORIZONTAL])
    ->render();

$data = [
    "name" => "json2html",
    "description" => "Converts JSON to HTML"
];

$table = (new RenderTable($data))
    ->config(['orientation' => TableOrientation::VERTICAL])
    ->render();

use Icmbio\Json2html\TableVerticalSeparate;

$table = TableVerticalSeparate::make($data)->render();

// Adicionar classe única
$table = (new RenderTable($data))
    ->tableClass('table table-bordered')
    ->render();

// Adicionar múltiplas classes via chaining
$table = (new RenderTable($data))
    ->tableClass('table')
    ->tableClass('table-bordered') 
    ->tableClass('table-hover')
    ->render();

$table = (new RenderTable($data))
    ->tableId('info-table')
    ->render();

$table = (new RenderTable($data))
    ->tableBorder(1)
    ->render();

$table = (new RenderTable($data))
    ->tableAttribute('data-test', 'custom')
    ->render();

$table = (new RenderTable($data))
    ->tableId('info-table')
    ->tableClass('table table-bordered')
    ->tableBorder(1)
    ->tableAttribute('data-test', 'combined')
    ->render();

$data = [
    "Monitor" => [
        ["Nome" => "Luiz Loureiro"]
    ]
];

$table = (new RenderTable($data))
    ->tableClass('root-only', nested: false)  // Boolean explícito
    ->render();

$table = (new RenderTable($data))
    ->tableClass('table', nested: true)  // Explícito (padrão)
    ->render();

$table = (new RenderTable($data))
    ->tableId('main-table')     // nested: false por padrão
    ->tableClass('table')       // nested: true por padrão  
    ->render();

$data = [
    "name" => "json2html",
    "description" => "Converts JSON to HTML"  
];

$table = (new RenderTable($data))
    ->config(['orientation' => TableOrientation::VERTICAL])
    ->tableClass('vertical-table')
    ->render();

// Construtor
new RenderTable(array $data)

// Configuração
->config(array $options): self

// Atributos da tabela
->tableClass(string $class, bool $nested = true): self
->tableId(string $id, bool $nested = false): self  
->tableBorder(int $border, bool $nested = true): self
->tableAttribute(string $name, string $value, bool $nested = true): self

// Renderização
->render(): string

// Enum com casos disponíveis  
TableOrientation::HORIZONTAL  // Padrão: headers no topo
TableOrientation::VERTICAL    // Headers na primeira coluna

$data = [
    "departamentos" => [
        ["nome" => "TI", "funcionarios" => 15],
        ["nome" => "RH", "funcionarios" => 8]  
    ]
];

$table = (new RenderTable($data))
    ->config(['orientation' => TableOrientation::HORIZONTAL])
    ->tableClass('table table-striped')
    ->tableId('departamentos-table') 
    ->tableBorder(1)
    ->render();

$data = [
    "usuarios" => [
        ["nome" => "João", "email" => "[email protected]"],
        ["nome" => "Maria", "email" => "[email protected]"]
    ]
];

$table = (new RenderTable($data))
    ->tableId('usuarios-table', nested: false)          // ID só na raiz
    ->tableClass('table', nested: true)                 // Classe em todas 
    ->tableClass('table-bordered', nested: false)       // Classe só na raiz
    ->tableBorder(1, nested: true)                      // Border em todas
    ->tableAttribute('data-module', 'users', nested: false)  // Atributo só na raiz
    ->render();