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

Twig tag or function inside PHP String

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

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

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

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

我正在尝试做的事情

控制器

$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
));

树枝

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

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

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

My variable contains "{{ image_src }}" 它将输出一个 string 表示:

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

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

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

Resuming,应该有一个 | compile twig 函数在变量内执行 Twig 代码,就像 | raw 对 HTML 所做的那样,但是,由于这个函数不存在,我我想知道是否有办法实现它...

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

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

编辑

我使用了我自己的 twig 扩展 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 class 适用于所有情况。

如果没有单独呈现字符串的 TwigExtension,你想做的事情在 twig 中是不可能的。

但是查看您的代码,您正试图以错误的方式使用 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