PHP code example of nystudio107 / craft-twig-sandbox
1. Go to this page and download the library: Download nystudio107/craft-twig-sandbox 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/ */
nystudio107 / craft-twig-sandbox example snippets
use nystudio107\crafttwigsandbox\web\SandboxView;
$sandboxView = new SandboxView();
use nystudio107\crafttwigsandbox\web\SandboxView;
use Twig\Sandbox\SecurityError;
$sandboxView = new SandboxView();
try {
$result = $sandboxView->renderTemplate();
} catch (\Throwable $e) {
// If this is a Twig Runtime exception, use the previous one instead
if ($e instanceof SecurityError && ($previousException = $e->getPrevious()) !== null) {
$e = $previousException;
}
// Exception handling here
}
use nystudio107\crafttwigsandbox\web\SandboxView;
$sandboxView = new SandboxView();
try {
$result = $sandboxView->renderTemplate();
} catch (\Throwable $e) {
$sandboxView->sandboxErrorHandler->handleException($e)
}
use nystudio107\crafttwigsandbox\twig\BlacklistSecurityPolicy;
use nystudio107\crafttwigsandbox\web\SandboxView;
$securityPolicy = new BlacklistSecurityPolicy([
'twigTags' => ['import'],
'twigFilters' => ['base64_decode', 'base64_encode'],
'twigFunctions' => ['dump'],
]);
$sandboxView = new SandboxView(['securityPolicy' => $securityPolicy]);
$result = $sandboxView->renderString("{{ dump() }}", []);
use craft\config\DbConfig;
use nystudio107\crafttwigsandbox\twig\BlacklistSecurityPolicy;
use nystudio107\crafttwigsandbox\web\SandboxView;
$securityPolicy = new BlacklistSecurityPolicy([
'twigProperties' => [
DbConfig::class => ['password']
],
'twigMethods' => [
DbConfig::class => ['getPassword']
],
]);
$sandboxView = new SandboxView(['securityPolicy' => $securityPolicy]);
$result = $sandboxView->renderString("{{ craft.app.config.db.password }}", []);