我的目的是对生成的html进行沙箱化。
$loader = new \Twig\Loader\ArrayLoader([
'test1.html.twig' => $this->getMessageObject()->getBody()
]);
$twig = new \Twig\Environment($loader);
$output = "{% sandbox %}{% include ("'.$twig->getLoader()->getSourceContext('test1.html.twig')->getPath().'") %} {% endsandbox %}"我无法从getPath()方法获得任何路径结果。是空绳子。
发布于 2022-09-09 12:22:02
要在不使用模板文件的情况下对小枝模板进行沙箱,可以使用链式加载器完成以下操作:
创建沙箱策略:
$tags = ['if'];
$filters = ['filter1', 'filter2'];
$methods = [
'Customer' => ['getCustomer'],
'Person' => ['getPerson']
];
$properties = [];
$functions = ['max'];
$policy = new \Twig\Sandbox\SecurityPolicy($tags, $filters, $methods, $properties, $functions);
$sandbox = new \Twig\Extension\SandboxExtension($policy);要在不创建Twig文件的情况下对Twig模板进行沙箱操作:
$loader1 = new \Twig\Loader\ArrayLoader([
'test1.html.twig' => "{% if 1==1 %} Tag Test {%endif%} test content here"
]);
$loader2 = new \Twig\Loader\ArrayLoader([
'sandbox.html.twig' => '{% sandbox %}{% include "test1.html.twig" %} {% endsandbox %}'
]);
$loader = new \Twig\Loader\ChainLoader([$loader1, $loader2]);
$twig = new \Twig\Environment($loader);
$twig->addExtension($sandbox);
$renderedOutput = $twig->render('sandbox.html.twig');https://stackoverflow.com/questions/73660078
复制相似问题