如何创建symfony twig过滤器?

时间:2012-08-10 18:19:52

标签: symfony twig

例如,我的包名称空间是Facebook\Bundle\FacebookBundle\Extension

使用此方法我如何创建树枝延伸?

4 个答案:

答案 0 :(得分:33)

一切都在这里:How to write a custom Twig Extension

<强> 1。创建扩展程序:

// src/Facebook/Bundle/Twig/FacebookExtension.php
namespace Facebook\Bundle\Twig;

use Twig_Extension;
use Twig_Filter_Method;

class FacebookExtension extends Twig_Extension
{
    public function getFilters()
    {
        return array(
            'myfilter' => new Twig_Filter_Method($this, 'myFilter'),
        );
    }

    public function myFilter($arg1, $arg2='')
    {
        return sprintf('something %s %s', $arg1, $arg2);
    }

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

<强> 2。注册分机即服务

# src/Facebook/Bundle/Resources/config/services.yml
services:
    facebook.twig.facebook_extension:
        class: Facebook\Bundle\Twig\AcmeExtension
        tags:
            - { name: twig.extension }

第3。使用它

{{ 'blah'|myfilter('somearg') }}

答案 1 :(得分:14)

您还可以使用getFunctions()

创建twig函数
class FacebookExtension extends Twig_Extension
{
    public function getFunctions()
    {
        return array(
            'myFunction' => new Twig_Filter_Method($this, 'myFunction'),
        );
    }

    public function myFunction($arg1)
    {
        return $arg1;
    }

使用您的功能:

{{ myFunction('my_param') }}

答案 2 :(得分:14)

自Symfony 2.1起,Twig_Filter_Method类 DEPRECATED

请使用 Twig_SimpleFilter 类,如以下示例所示:

\ SRC \ Acme公司\捆绑\ CoreBundle \枝条\ DatetimeExtension.php

<?php

namespace Acme\Bundle\CoreBundle\Twig;

use Symfony\Component\DependencyInjection\ContainerInterface;

class DatetimeExtension extends \Twig_Extension
{
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getFilters()
    {
        return array(
            'dateFormat' => new \Twig_SimpleFilter('dateFormat', array($this, 'dateFormat')),
            'datetimeFormat' => new \Twig_SimpleFilter('datetimeFormat', array($this, 'datetimeFormat'))
        );
    }

    /**
     * @param mixed $date
     * @return string
     */
    public function dateFormat($date)
    {
        $format = $this->container->getParameter('acme_core.date_format');
        return $this->format($date, $format);
    }

    /**
     * @param mixed $date
     * @return string
     */
    public function datetimeFormat($date)
    {
        $format = $this->container->getParameter('acme_core.datetime_format');
        return $this->format($date, $format);
    }

    /**
     * @param mixed $date
     * @param string $format
     * @throws \Twig_Error
     * @return string
     */
    private function format($date, $format)
    {
        if (is_int($date) || (is_string($date) && preg_match('/^[0-9]+$/iu', $date))) {
            return date($format, intval($date, 10));
        } else if (is_string($date) && !preg_match('/^[0-9]+$/', $date)) {
            return date($format, strtotime($date));
        } else if ($date instanceof \DateTime) {
            return $date->format($format);
        } else {
            throw new \Twig_Error('Date or datetime parameter not valid');
        }
    }

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

\ SRC \ Acme公司\捆绑\ CoreBundle \资源\配置\ services.yml

services:
    acme_core.twig.datetime_extension:
        class: Acme\Bundle\CoreBundle\Twig\DatetimeExtension
        arguments: [@service_container]
        tags:
            - { name: twig.extension }

用法示例:

{{ value|datetimeFormat }}

Symfony文档:http://symfony.com/doc/master/cookbook/templating/twig_extension.html

Twig文档:http://twig.sensiolabs.org/doc/advanced.html#id3

答案 3 :(得分:2)

给出的答案均不适用于Symfony 3.4及更高版本。

对于Symfony 3.4、4.x

// src/TwigExtension/customFilters.php 

namespace App\TwigExtension; 
use Twig\TwigFilter; 

class customFilters extends \Twig_Extension { 

    public function getFilters() { 
        return array( 
            new TwigFilter('base64_encode', array($this, 'base64_en'))
        );
    }

    public function base64_en($input) {
       return base64_encode($input);
    }
}

然后在您的树枝模板中可以完成

{{ 'hello world' | base64_encode }}

就这样。有关详细说明,您可以查看参考。

参考 DigitalFortress