Javascript twig是安全的,因为简单功能不起作用
我创建了一个简单的函数,用js呈现一个模板。 我希望它能够自动转义,因此我将is|u safe参数设置为array(html'),以避免使用| raw过滤器 但是它不起作用,JSF不会转义,而是呈现为纯文本。如果我使用| raw过滤器,它就可以正常工作 我怎样才能解决这个问题 我的简单功能:Javascript twig是安全的,因为简单功能不起作用,javascript,symfony,filter,escaping,twig,Javascript,Symfony,Filter,Escaping,Twig,我创建了一个简单的函数,用js呈现一个模板。 我希望它能够自动转义,因此我将is|u safe参数设置为array(html'),以避免使用| raw过滤器 但是它不起作用,JSF不会转义,而是呈现为纯文本。如果我使用| raw过滤器,它就可以正常工作 我怎样才能解决这个问题 我的简单功能: <?php namespace AppBundle\Extension\Twig; use AppBundle\FoodMeUpParameters; use AppBundle\Model\In
<?php
namespace AppBundle\Extension\Twig;
use AppBundle\FoodMeUpParameters;
use AppBundle\Model\Interfaces\ViewCountInterface;
use ReflectionClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FMUTwigExtension extends \Twig_Extension
{
/**
* @var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
public function getFunctions()
{
return array(
'increaseViewCount' => new \Twig_SimpleFunction('increaseViewCount', array($this, 'increaseViewCount', array('is_safe' => array('html')))),
);
}
public function increaseViewCount(ViewCountInterface $entity, $andFlush = true)
{
$reflect = new ReflectionClass($entity);
$parameters = array(
'short_name' => $reflect->getShortName(),
'identifier' => $entity->getId(),
'and_flush' => $andFlush
);
return $this->container->get('templating')->render(':Helper:increase_view_count.htmpl.twig', $parameters);
}
}
我想,在您的情况下,您应该选择
'is_safe'=>['js']
。如果由于某种原因没有帮助,您可以使用Twig的filterraw
,其中是。您在回调中包含了'is_safe'
选项,而不是在下一个($options
)参数中
你需要改变
'increaseViewCount' => new \Twig_SimpleFunction('increaseViewCount', array($this, 'increaseViewCount', array('is_safe' => array('html')))),
。。。到
'increaseViewCount' => new \Twig_SimpleFunction('increaseViewCount', array($this, 'increaseViewCount'), array('is_safe' => array('html'))),
嗯,将这个“html”值更改为“js”或“all”不会改变任何东西:js显示为纯文本,而不是在使用原始过滤器时执行。。。我仍然困在这里。你知道要测试什么才能更好地理解问题吗?只是犯了同样的错误。你帮我节省了时间。
'increaseViewCount' => new \Twig_SimpleFunction('increaseViewCount', array($this, 'increaseViewCount', array('is_safe' => array('html')))),
'increaseViewCount' => new \Twig_SimpleFunction('increaseViewCount', array($this, 'increaseViewCount'), array('is_safe' => array('html'))),