PHP code example of guansunyata / server-side-rendering

1. Go to this page and download the library: Download guansunyata/server-side-rendering 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/ */

    

guansunyata / server-side-rendering example snippets


use Spatie\Ssr\Renderer;
use Spatie\Ssr\Engines\V8;

$engine = new V8();

$renderer = new Renderer($engine);

echo $renderer
    ->entry(__DIR__.'/../../public/js/app-server.js')
    ->render();

// <div>My server rendered app!</div>

echo $renderer
    ->disabled($disabled)
    ->context('user', $user)
    ->entry(__DIR__.'/../../public/js/app-server.js')
    ->render();

echo $renderer
    ->entry(__DIR__.'/../../public/js/app-server.js')
    ->context('user', ['name' => 'Sebastian'])
    ->render();

$renderer->context('user', ['name' => 'Sebastian']);

$renderer->context(['user' => ['name' => 'Sebastian']]);

$renderer->env('NODE_ENV', 'production');

$renderer->env(['NODE_ENV' => 'production']);

$renderer->fallback('<div id="app"></div>');

echo $renderer
    ->resolveEntryWith(function (string $entry): string {
        return __DIR__."/../../public/js/{$entry}-server.js";
    })
    ->entry('app')
    ->render();