21

例如我的包命名空间是Facebook\Bundle\FacebookBundle\Extension.

使用它如何创建树枝扩展?

4

4 回答 4

34

都在这里:如何编写自定义 Twig 扩展

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') }}
于 2012-08-10T20:07:16.920 回答
16

您还可以使用 getFunctions() 创建树枝函数

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') }}
于 2012-08-10T23:09:29.640 回答
15

Twig_Filter_Method 类自 Symfony 2.1 起已弃用

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

\src\Acme\Bundle\CoreBundle\Twig\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\Bundle\CoreBundle\Resources\config\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

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

于 2014-03-05T13:34:37.233 回答
2

None of the given answers worked for Symfony 3.4 and above.

For 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);
    }
}

And then in your twig template you can do

{{ 'hello world' | base64_encode }}

Thats it. For detailed explanation, you could check out the reference.

Reference: DigitalFortress

于 2018-11-16T16:08:36.113 回答