Twig 扩展禁用自动转义
Twig extension disable auto escape
我做了一个 twig 扩展以便能够调用模板中的函数,但不幸的是现在这些函数的 html 渲染内容被转义了。你知道我如何禁用它吗?
FrontendTwigExtension.php
class FrontendTwigExtension extends Twig_Extension
{
/**
* @var DataProviderService
*/
private $dataProvider;
/**
* FrontendTwigExtension constructor.
* @param DataProviderService $dataProvider
*/
public function __construct(DataProviderService $dataProvider)
{
$this->dataProvider = $dataProvider;
}
/**
* @return array
*/
public function getFunctions(): array
{
return array(
new Twig_Function('getProductDetailData',
[$this, 'getProductDetailData'],
['needs_environment' => true]
),
new Twig_Function('getPageHeader',
[$this, 'getPageHeader'],
['needs_environment' => true]
)
);
}
/**
* @param Twig_Environment $env
* @return string
* @throws Twig_Error_Loader
* @throws Twig_Error_Runtime
* @throws Twig_Error_Syntax
*/
public function getPageHeader(Twig_Environment $env): string
{
return $env->render('Component/PageHeader/pageHeader.html.twig');
}
/**
* @param Twig_Environment $env
* @return string
* @throws Twig_Error_Loader
* @throws Twig_Error_Runtime
* @throws Twig_Error_Syntax
*/
public function getProductDetailData(Twig_Environment $env): string
{
$service = new ProductDetailDataService($this->dataProvider);
return $env->render('Module/ProductDetailPage/productDetailData.html.twig',
[
'productData' => $service->getData()
]
);
}
}
template.html.twig
{% extends 'base.html.twig' %}
{% block pageHeader %}
{{ getPageHeader() }}
{{ getProductDetailData() }}
{% endblock %}
services.yaml
App\Extension\FrontendTwigExtension:
arguments:
- '@App\DataProvider\DataProviderService'
tags:
- { name: 'twig.extension' }
您需要设置is_safe
选项:
['needs_environment' => true, 'is_safe' => ['html']]
尝试在 twig 中使用 raw
函数。
{% extends 'base.html.twig' %}
{% block pageHeader %}
{{ getPageHeader()|raw }}
{{ getProductDetailData()|raw }}
{% endblock %}
我做了一个 twig 扩展以便能够调用模板中的函数,但不幸的是现在这些函数的 html 渲染内容被转义了。你知道我如何禁用它吗?
FrontendTwigExtension.php
class FrontendTwigExtension extends Twig_Extension
{
/**
* @var DataProviderService
*/
private $dataProvider;
/**
* FrontendTwigExtension constructor.
* @param DataProviderService $dataProvider
*/
public function __construct(DataProviderService $dataProvider)
{
$this->dataProvider = $dataProvider;
}
/**
* @return array
*/
public function getFunctions(): array
{
return array(
new Twig_Function('getProductDetailData',
[$this, 'getProductDetailData'],
['needs_environment' => true]
),
new Twig_Function('getPageHeader',
[$this, 'getPageHeader'],
['needs_environment' => true]
)
);
}
/**
* @param Twig_Environment $env
* @return string
* @throws Twig_Error_Loader
* @throws Twig_Error_Runtime
* @throws Twig_Error_Syntax
*/
public function getPageHeader(Twig_Environment $env): string
{
return $env->render('Component/PageHeader/pageHeader.html.twig');
}
/**
* @param Twig_Environment $env
* @return string
* @throws Twig_Error_Loader
* @throws Twig_Error_Runtime
* @throws Twig_Error_Syntax
*/
public function getProductDetailData(Twig_Environment $env): string
{
$service = new ProductDetailDataService($this->dataProvider);
return $env->render('Module/ProductDetailPage/productDetailData.html.twig',
[
'productData' => $service->getData()
]
);
}
}
template.html.twig
{% extends 'base.html.twig' %}
{% block pageHeader %}
{{ getPageHeader() }}
{{ getProductDetailData() }}
{% endblock %}
services.yaml
App\Extension\FrontendTwigExtension:
arguments:
- '@App\DataProvider\DataProviderService'
tags:
- { name: 'twig.extension' }
您需要设置is_safe
选项:
['needs_environment' => true, 'is_safe' => ['html']]
尝试在 twig 中使用 raw
函数。
{% extends 'base.html.twig' %}
{% block pageHeader %}
{{ getPageHeader()|raw }}
{{ getProductDetailData()|raw }}
{% endblock %}