Symfony 从Twig模板调用PHP函数

Symfony 从Twig模板调用PHP函数,symfony,twig,symfony-2.3,Symfony,Twig,Symfony 2.3,我的控制器中有一个返回实体数组的函数,因此在我的细枝模板中,我这样做是为了迭代元素: {% for groupName, entity in items %} <ul> <ul> {% for element in entity %} <li>{{ element.getLabel }}</li> <li><input

我的控制器中有一个返回实体数组的函数,因此在我的细枝模板中,我这样做是为了迭代元素:

{% for groupName, entity in items %}
    <ul>
        <ul>
            {% for element in entity %}
                <li>{{ element.getLabel }}</li>
                <li><input type="text" name="detail[{{ element.getId }}]" id="pd_{{ element.getId }}" /><input type="text" name="price[{{ element.getId }}]" id="pd_price_{{ element.getId }}" /><input type="text" name="stock[{{ element.getId }}]" id="pd_stock_{{ element.getId }}" /></li>
            {% endfor %}
        </ul>
    </ul>
{% endfor %}

不可能直接访问Twig中的任何PHP函数

您可以做的是编写一个细枝扩展。一种常见的结构是,编写带有一些实用功能的服务, 将细枝扩展写为桥,以从细枝访问服务。Twig分机将使用该服务,并且 您的控制器也可以使用该服务

看一看:


干杯。

已经有了一个细枝扩展,可以从细枝模板中调用PHP函数,如:

Hi, I am unique: {{ uniqid() }}.

And {{ floor(7.7) }} is floor of 7.7.

参见官方。

您可以通过

$arr = get_defined_functions();
print_r($arr);
这将为您提供中所有函数的数组,如果其中存在您的函数,您可以像以下方式使用它:

 {{ user.myfunction({{parameter}}) }}

虽然我同意关于从控制器传入变量的评论,但在设置twig环境时,您也可以注册未定义的函数

$twig->registerUndefinedFunctionCallback(function ($name) {
        // security
        $allowed = false;
        switch ($name) {
            // example of calling a wordpress function
            case 'get_admin_page_title':
                $allowed = true;
                break;
        }

        if ($allowed && function_exists($name)) {
            return new Twig_Function_Function($name);
        }

        return false;
    });
这是从


没有按照原始问题的要求对对象调用函数

如果您真的知道自己在做什么,并且不介意邪恶的方式,这是您唯一需要的附加细枝扩展:

function evilEvalPhp($eval, $args = null)
{
    $result = null;
    eval($eval);
    return $result;
}

我很惊讶代码的答案还没有公布,它只是一行

你可以
{{categorey_id | getvariances}}

这是一行:
第二条:

小枝3:

$this->twig->addFilter(new \Twig\TwigFilter('getVariations','getVariations'));
细枝3但作为功能而不是过滤器:

$this->twig->addFunction(new \Twig\TwigFunction('getVariantsFunc', 'getVariations'));

我知道这是一根旧线。但在symfony 3.3中,我做到了:

{{ render(controller(
    'AppBundle\\Controller\\yourController::yourAction'
)) }}

这对我很有效(使用):


这将创建一个名为
md5
的新过滤器,它将返回参数的值。

为什么不在控制器中正手处理它,并将
$items
$combinations
发送到视图?@ManuelGutierrez我尝试过,但不知道如何处理,对此有什么想法吗?像往常一样从数据库中获取
$items
,然后将其传递给控制器上的
DetailCombination()
函数,将其保存在另一个变量
$combinations
上,并将两者传递到视图,所有这些步骤都将在同一个控制器函数上。您的答案是错误的,createproblems-answer不会导致任何安全问题,如您在此处发布的离题PHP代码。你的非主题小枝评论也是错误的。我甚至无法想象现在有些人是如何用PHP作为模板引擎来破坏他们的项目的。它在很久以前就已经发展出来了,并且是制作绝对糟糕代码的最佳方法之一。John,为什么有人会认为视图层中的PHP代码会创建糟糕的代码?那是谎言。看看Zend Framkework 1和2()或Wordpress等中的默认视图渲染器。我的php代码没有脱离主题,因为它允许从Twig模板调用php函数!谢谢,这对我很有帮助!您能告诉我如何定义/传递回调函数吗?i、 e在细枝模板中调用数组映射PHP函数-{{function('array_map',callback,myarray)},可能吗?
$twig->addFilter('getVariations', new Twig_Filter_Function('getVariations'));
$this->twig->addFilter(new \Twig\TwigFilter('getVariations','getVariations'));
$this->twig->addFunction(new \Twig\TwigFunction('getVariantsFunc', 'getVariations'));
{{ render(controller(
    'AppBundle\\Controller\\yourController::yourAction'
)) }}
$twig->getEnvironment()->addFilter(
   new \Twig_Filter('md5', function($arg){ return md5($arg); })
);