首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ArrayLoader的Symfony Twig太极拳

使用ArrayLoader的Symfony Twig太极拳
EN

Stack Overflow用户
提问于 2022-09-09 09:32:03
回答 1查看 73关注 0票数 -1

我的目的是对生成的html进行沙箱化。

代码语言:javascript
复制
$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()方法获得任何路径结果。是空绳子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-09 12:22:02

要在不使用模板文件的情况下对小枝模板进行沙箱,可以使用链式加载器完成以下操作:

创建沙箱策略:

代码语言:javascript
复制
$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模板进行沙箱操作:

代码语言:javascript
复制
$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');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73660078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档