PHP code example of icetea / icecube

1. Go to this page and download the library: Download icetea/icecube 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/ */

    

icetea / icecube example snippets




namespace App\Components;

use IceTea\IceCube\SingleFileComponent;
use IceTea\IceDOM\HtmlNode;

class Counter extends SingleFileComponent
{
  public function __construct(
    public int $initialCount = 0,
    public string $label = 'Counter',
  ) {}

  public function render(): HtmlNode
  {
    return _div(['class' => 'counter-container'], [
      _h2($this->label),
      _p([
        _span('Count: '),
        _strong(['class' => 'count-value'], [
          _span($this->initialCount)->data_ref('counter'),
        ]),
      ]),
      _div([
        _button(['class' => 'btn'], '-')->data_ref('decrementBtn'),
        _button(['class' => 'btn'], '+')->data_ref('incrementBtn'),
      ], ['class' => 'button-group']),
    ]);
  }
}


use IceTea\IceCube\Compiler\IceCubeCompiler;
use IceTea\IceCube\Compiler\EmbedStyleScriptCompiler;

$compiler = new IceCubeCompiler(
    prefixClass: 'App\\Components',
    sourceDir: __DIR__ . '/../app/Components',
    compiledPhpDir: storage_path('icecube'),
    compiledAssetsDir: public_path('icecube'),
    publicUrl: '/icecube',
    scriptCompiler: new EmbedStyleScriptCompiler()
);

// For development: compile on-demand via autoloader (automatic)
// For production: pre-compile all components
$compiler->scanAndCompile();

<!DOCTYPE html>
<html>
<head>
    <title>My App</title>
    <?= IceCubeRegistry::allStyles() 

// During build/deployment
use IceTea\IceCube\IceCubeRegistry;

$compiler->scanAndCompile();
IceCubeRegistry::storeCache(storage_path('icecube/cache.php'));

// In production bootstrap
IceCubeRegistry::loadCache(storage_path('icecube/cache.php'));

@vite(['resources/js/app.js', 'resources/js/icecube.js'])



_head([
  _safe(Vite::withEntryPoints(['resources/js/app.js', 'resources/js/icecube.js'])->toHtml()),
])

use IceTea\IceCube\Compiler\IceCubeCompiler;
use IceTea\IceCube\Compiler\ViteScriptCompiler;
use IceTea\IceCube\Compiler\ScssStyleCompiler;

// Optional: Configure SCSS with custom import paths
$styleCompiler = new ScssStyleCompiler();
$scss = $styleCompiler->getCompiler();
$scss->addImportPath(base_path('app/Components'));

$compiler = new IceCubeCompiler(
    prefixClass: 'App\\Components',
    sourceDir: base_path('app/Components'),
    compiledPhpDir: storage_path('app/private/icecube'),
    compiledAssetsDir: storage_path('app/public/icecube'),
    publicUrl: '/storage/icecube',
    scriptCompiler: new ViteScriptCompiler(),
    styleCompiler: $styleCompiler, // Optional: for SCSS support
);

$compiler->scanAndCompile();

// When using Vite, skip storing styles in cache (they're handled by Vite)
IceCubeRegistry::storeCache(
    storage_path('app/private/icecube/cache.php'),
    

'providers' => [
    // ...
    IceTea\IceCube\Laravel\IceCubeServiceProvider::class,
],

return [
    // ...
    IceTea\IceCube\Laravel\IceCubeServiceProvider::class,
];

return [
    'compilers' => [
        'default' => [
            'prefix_class' => 'App\\Components',
            'source_dir' => base_path('app/Components'),
            'compiled_php_dir' => storage_path('app/private/icecube'),
            'compiled_assets_dir' => storage_path('app/public/icecube'),
            'public_url' => '/storage/icecube',
            'script_compiler' => ViteScriptCompiler::class,
            'style_compiler' => ScssStyleCompiler::class,
            'cache_enabled' => env('ICECUBE_CACHE', true),
            'cache_file' => storage_path('app/private/icecube/cache.php'),
        ],
    ],
];

use IceTea\IceCube\Compiler\ScssStyleCompiler;

$styleCompiler = app(ScssStyleCompiler::class);
$scss = $styleCompiler->getCompiler();
$scss->addImportPath(base_path('app/Components'));

// Controller
public function loadComponent(Request $request)
{
    return (string) new Counter($request->count, 'Dynamic');
}

return [
    'compilers' => [
        'main' => [
            'prefix_class' => 'App\\Components',
            'source_dir' => base_path('app/Components'),
            'compiled_php_dir' => storage_path('app/private/icecube/main'),
            'compiled_assets_dir' => storage_path('app/public/icecube/main'),
            'public_url' => '/storage/icecube/main',
            'script_compiler' => ViteScriptCompiler::class,
            'style_compiler' => ScssStyleCompiler::class,
            'cache_enabled' => true,
            'cache_file' => storage_path('app/private/icecube/main-cache.php'),
        ],

        'admin' => [
            'prefix_class' => 'App\\Admin\\Components',
            'source_dir' => base_path('app/Admin/Components'),
            'compiled_php_dir' => storage_path('app/private/icecube/admin'),
            'compiled_assets_dir' => storage_path('app/public/icecube/admin'),
            'public_url' => '/storage/icecube/admin',
            'script_compiler' => EmbedStyleScriptCompiler::class,
            'style_compiler' => NestingStyleCompiler::class,
            'cache_enabled' => true,
            'cache_file' => storage_path('app/private/icecube/admin-cache.php'),
        ],
    ],
];

class MyComponent extends SingleFileComponent
{
  public function __construct(
    public string $title,      // Passed to client
    public array $items,       // Passed to client
    private string $secret,    // NOT passed to client
  ) {}
}

_button('Click me')->data_ref('myButton')

<style>
  /* Scoped: only affects this component */
  .button { color: blue; }
</style>

<style global>
  /* Global: affects entire page */
  body { margin: 0; }
</style>

use IceTea\IceCube\Compiler\ScssStyleCompiler;

$compiler = new IceCubeCompiler(
    // ... other params
    styleCompiler: new ScssStyleCompiler()
);

<style>
  $primary-color: #007bff;

  & {
    background: $primary-color;

    .nested {
      color: darken($primary-color, 10%);
    }
  }
</style>

.ice.php file
    ↓
IceCubeParser
    ↓
ParsedComponent (PHP + Styles + JS)
    ↓
IceCubeCompiler
    ├→ StyleCompiler → compiled.css
    ├→ ScriptCompiler → compiled.js (with styles)
    └→ PHP class → compiled.php
    ↓
CompiledComponent
    ↓
IceCubeRegistry
bash
php artisan vendor:publish --tag=icecube
bash
php artisan storage:link
bash
php artisan icecube:compile
bash
# Deploy script
composer install --optimize-autoloader --no-dev
php artisan icecube:compile
npm run build  # Vite handles CSS bundling
php artisan config:cache
php artisan route:cache
blade
<!-- View -->
<div hx-get="/load-component?count=100" hx-swap="outerHTML">
    Load Component
</div>
bash
php artisan icecube:compile
bash
php artisan icecube:compile --compiler=admin