A library for server rendering web components in PHP that is compatible with Enhance SSR (Enhance.dev).
This package includes both a WASM and native PHP version of enhance ssr. The WASM version allows component definitions written in JavaScript, but has specific requirements for the hosting environment that may be challenging. The examples directory includes examples of both versions.
This package can be managed and installed with Composer:
composer require enhance-dev/ssr
The Extism dependency currently requires "minimum-stability":"dev"
in the composer.json
file.
To run the native and WASM examples run composer serve-native
or composer serve-wasm
respectively.
See usage examples for native PHP and WASM in the examples directory.
<?php
require "../../../vendor/autoload.php";
use Enhance\Enhancer;
use Enhance\Elements;
use Enhance\ShadyStyles;
$elementPath = __DIR__ . "/../resources";
$elements = new Elements($elementPath);
$scopeMyStyle = new ShadyStyles();
$enhance = new Enhancer([
"elements" => $elements,
"initialState" => [],
"styleTransforms" => [[$scopeMyStyle, "styleTransform"]],
"enhancedAttr" => true,
"bodyContent" => false,
]);
$htmlString = <<<HTMLDOC
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<my-header><h1>Hello World</h1></my-header>
</body>
</html>
HTMLDOC;
$output = $enhance->ssr($htmlString);
echo $output;
<?php
require "../../../vendor/autoload.php";
use Enhance\EnhanceWASM;
use Enhance\Elements;
$elementPath = "../resources";
$elements = new Elements($elementPath, ["wasm" => true]);
$enhance = new EnhanceWASM(["elements" => $elements->wasmElements]);
$input = [
"markup" => "<my-header>Hello World</my-header>",
"initialState" => [],
];
$output = $enhance->ssr($input);
$htmlDocument = $output->document;
echo $htmlDocument . "\n";
For the WASM version there are additional requirements. For this library, you first need to install the Extism Runtime by following the instructions in the PHP SDK Repository.
Thank you @mariohamann for prototyping a PHP example in using Extism https://github.com/mariohamann/enhance-ssr-wasm/tree/experiment/extism.