Symfony 如何动态调用细枝过滤器

Symfony 如何动态调用细枝过滤器,symfony,twig,Symfony,Twig,我需要使用特定于每个数据类型的过滤器呈现未知类型的数据: 渲染的结构如下所示: array( "value" => "value-to-render", "filter" => "filter-to-apply", ) {% for item in items %} {{ item.value|item.filter|raw}} {% endfor %} 因此我的问题是:如何让twig使用item.filter作为值的过滤器?您必须编写过滤器,它将通过向

我需要使用特定于每个数据类型的过滤器呈现未知类型的数据:

渲染的结构如下所示:

array(
    "value"  => "value-to-render",
    "filter" => "filter-to-apply",
)

{% for item in items %}
    {{ item.value|item.filter|raw}}
{% endfor %}

因此我的问题是:如何让twig使用item.filter作为值的过滤器?

您必须编写过滤器,它将通过向其传递名称来调用过滤器

如何开始编写您可以阅读的扩展名

假设您已经创建了扩展,您已经定义了自定义函数(例如,
customFilter

然后,您必须定义这个函数

public function customFilter($context, $filterName)
{
    // handle parameters here, by calling the 
    // appropriate filter and pass $context there
}
完成此操作后,您将能够调用Twig:

{% for item in items %}
    {{ custom_filter(item.value, item.filter)|raw  }}
{% endfor %}
或者,如果您已将过滤器定义为过滤器(而不是函数):


您必须编写过滤器,它将通过向过滤器传递名称来调用过滤器

如何开始编写您可以阅读的扩展名

假设您已经创建了扩展,您已经定义了自定义函数(例如,
customFilter

然后,您必须定义这个函数

public function customFilter($context, $filterName)
{
    // handle parameters here, by calling the 
    // appropriate filter and pass $context there
}
完成此操作后,您将能够调用Twig:

{% for item in items %}
    {{ custom_filter(item.value, item.filter)|raw  }}
{% endfor %}
或者,如果您已将过滤器定义为过滤器(而不是函数):


这个问题与我的一个问题直接相关:


答案是你需要一种“评估”方法,这种方法目前还不存在(但很快就会存在)。但您也可以创建自己的函数,如@TheCatonOfflat所述。

此问题与我的一个问题直接相关:


答案是你需要一种“评估”方法,这种方法目前还不存在(但很快就会存在)。但您也可以创建自己的函数,如@theCatonOfflat所述。

我刚刚为此创建了一个Symfony包:


看看这里:

我刚刚为此创建了一个Symfony包:


看看这里:

这根树枝为我做了一件好事:

<?php

namespace YourNamespace\YourBundle\Twig;

use \Twig_Extension;
use \Twig_SimpleFilter;
use \Twig_Environment;

class ApplyFilterExtension extends Twig_Extension
{
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'apply_filter_twig_extension';
    }

    public function getFilters()
    {
        return array(
            new Twig_SimpleFilter('apply_filter', array($this, 'applyFilter'), [
                    'needs_environment' => true,
                ]
            ));
    }

    public function applyFilter(Twig_Environment $env, $value, $filterName)
    {
        $twigFilter = $env->getFilter($filterName);

        if (!$twigFilter) {
            return $value;
        }

        return call_user_func($twigFilter->getCallable(), $value);
    }
}

这根树枝的延伸帮了我的忙:

<?php

namespace YourNamespace\YourBundle\Twig;

use \Twig_Extension;
use \Twig_SimpleFilter;
use \Twig_Environment;

class ApplyFilterExtension extends Twig_Extension
{
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'apply_filter_twig_extension';
    }

    public function getFilters()
    {
        return array(
            new Twig_SimpleFilter('apply_filter', array($this, 'applyFilter'), [
                    'needs_environment' => true,
                ]
            ));
    }

    public function applyFilter(Twig_Environment $env, $value, $filterName)
    {
        $twigFilter = $env->getFilter($filterName);

        if (!$twigFilter) {
            return $value;
        }

        return call_user_func($twigFilter->getCallable(), $value);
    }
}

以下是dossorio答案的扩展,它允许链接多个过滤器,并在需要时向过滤器传递额外参数:

class applyFiltersExtension extends Twig_Extension
{
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'apply_filters_twig_extension';
    }

    public function getFilters()
    {
        return [
            new Twig_SimpleFilter('apply_filters', [$this, 'applyFilters'], [
                    'needs_environment' => true,
                ]
            )];
    }

    public function applyFilters(Twig_Environment $env, $value, array $filters = null)
    {
        if (empty($filters)) {
            return $value;
        }

        foreach ($filters as $filter) {
            if (is_array($filter)) {
                $filter_name = array_shift($filter);
                $params      = array_merge([$env, $value], $filter);
            } else {
                $filter_name = $filter;
                $params      = [$env, $value];
            }
            $twigFilter = $env->getFilter($filter_name);
            if (empty($twigFilter)) {
                continue;
            }

            $value = call_user_func_array($twigFilter->getCallable(), $params);
        }

        return $value;
    }
}

.

这是dossorio答案的扩展,允许链接多个过滤器,并在需要时向过滤器传递额外参数:

class applyFiltersExtension extends Twig_Extension
{
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'apply_filters_twig_extension';
    }

    public function getFilters()
    {
        return [
            new Twig_SimpleFilter('apply_filters', [$this, 'applyFilters'], [
                    'needs_environment' => true,
                ]
            )];
    }

    public function applyFilters(Twig_Environment $env, $value, array $filters = null)
    {
        if (empty($filters)) {
            return $value;
        }

        foreach ($filters as $filter) {
            if (is_array($filter)) {
                $filter_name = array_shift($filter);
                $params      = array_merge([$env, $value], $filter);
            } else {
                $filter_name = $filter;
                $params      = [$env, $value];
            }
            $twigFilter = $env->getFilter($filter_name);
            if (empty($twigFilter)) {
                continue;
            }

            $value = call_user_func_array($twigFilter->getCallable(), $params);
        }

        return $value;
    }
}

.

应用过滤器不是这样工作的,但它起了作用:
{{item.value | apply_filter(item.filter)| raw}
我猜,因为您将其定义为过滤器,而不是函数(如我所述)。在这种情况下,
item.value
自动作为
$context
传递到您的筛选器中:)您是如何从CustomFilter调用筛选器的?如何从PHP调用twig扩展?应用过滤器不是这样工作的,但它起了作用:
{{item.value | apply_filter(item.filter)| raw}
我猜,因为您已经将其定义为过滤器,而不是函数(如我所述)。在这种情况下,
item.value
自动作为
$context
传递到您的筛选器中:)您是如何从CustomFilter调用筛选器的?如何从PHP调用细枝扩展?