如何创建symfony twig过滤器?

问题描述 投票:20回答:4

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

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

symfony twig
4个回答
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') }}

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') }}

14
投票

自Symfony 2.1以来,Twig_Filter_Method类已被弃用

请使用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


2
投票

没有给出的答案适用于Symfony 3.4及以上版本。

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

然后在你的枝条模板中你可以做到

{{ 'hello world' | base64_encode }}

而已。有关详细说明,您可以查看参考。

参考:DigitalFortress

© www.soinside.com 2019 - 2024. All rights reserved.