PHP字符串中的Twig标记或函数

时间:2015-10-02 07:50:12

标签: php symfony twig

有没有办法使用这样的东西?

$foo = "{{ object|filter }}";

因为我正在尝试编写一个需要输出类似示例的动态图像转换器,但是当我在我的树枝中使用{{ foo }}时,它只输出一个原始字符串{{ object|filter }}而不是执行按预期过滤对象。

我尝试使用{{ foo | raw }}但结果相同。

我正在努力做的事情

CONTROLLER

$image = $em->getRepository('AcmeDemo:Media')->find($id);
$image_src = sprintf("{{ %s | imagine_filter('%s') }}", $image->getWebPath(), 'front_small');
return $this->render('image.html.twig', array(
    'image_src' => $image_src
));

TWIG

<img src="{{ image_src }}"/>

所以,我在PHP变量$image_src中有一个twig函数,一旦用sprintf {{ '/uploads/foo.jpg' | imagine_filter('front_small') }}格式化,就可以使用Twig函数。

这是一个现在的字符串,因为它在一个php变量$image_src内,该变量被发送到我的名为image_src的Twig模板,所以,现在它是一个字符串,因为我说,如果我做了

My variable contains "{{ image_src }}"它会输出一个字符串,上面写着:

My variable contains "{{ '/uploads/foo.jpg' | imagine_filter('front_small') }}"

因为,正如我所说,image_src只是一个字符串,但我想在我的Twig中执行,包含image_src的字符串,因为是的,它是一个字符串(对编译器的眼睛)但我们都知道它是假装是Twig函数(因为语法)。

那么,为什么| raw 不会工作?,因为它打算与包含HTML代码的字符串一起使用,如果它是HTML语法的话可行,但它是Twig语法,所以它不起作用。

恢复,应该有一个| compile twig函数在像| raw这样的变量中执行Twig代码与HTML一样,但是,由于这个函数不存在,我想知道是否有实现它的方法......

正如@joshua所说,它就像一个Javascript eval

我希望我已经解释了问题是什么以及我需要什么。

修改

我已经使用了自己的枝条扩展Compile来实现我所需要的。

class CompileExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'compile'   => new \Twig_Filter_Method($this, 'compile', array(
                'needs_environment' => true,
                'needs_context'     => true,
                'is_safe'   => array('compile' => true)
            )),
        );
    }

    public function compile(\Twig_Environment $environment, $context, $string)
    {
        $loader = $environment->getLoader();

        $compiled = $this->compileString($environment, $context, $string);

        $environment->setLoader($loader);
        return $compiled;
    }

    public function compileString(\Twig_Environment $environment, $context, $string)
    {
        $environment->setLoader(new \Twig_Loader_String());
        return $environment->render($string, $context);
    }

    public function getName()
    {
        return 'compile';
    }
}

更新 接受@Benjamin Paap的答案,因为它在这种情况下完全符合我想要的更好的代码,但我的自定义Twig类适用于所有情况。

1 个答案:

答案 0 :(得分:1)

如果没有TwigExtension分别渲染你的字符串,你想要做的事情是不可能的。

但是看看你的代码,你试图以错误的方式使用LiipImagineBundle。以这种方式使用它似乎很诱人,但为缩略图生成URL的正确方法是:

class MyController extends Controller
{
    public function indexAction()
    {
        // RedirectResponse object
        $imagemanagerResponse = $this->container
            ->get('liip_imagine.controller')
            ->filterAction(
                $this->request,         // http request
                'uploads/foo.jpg',      // original image you want to apply a filter to
                'my_thumb'              // filter defined in config.yml
            );

        // string to put directly in the "src" of the tag <img>
        $cacheManager = $this->container->get('liip_imagine.cache.manager');
            $srcPath = $cacheManager->getBrowserPath('uploads/foo.jpg', 'my_thumb');

        // ..
    }
}

https://github.com/liip/LiipImagineBundle#using-the-controller-as-a-service

相关问题