The main motivation for writing an extension is to move often used code into a reusable class like adding support for internationalization. An extension can define tags, filters, tests, operators, global variables, functions, and node visitors.
创建扩展的主要目的是将常用代码移入一个可复用的类中,如添加国际化支持那样。一个扩展可以定义标签、过滤器、测试、操作、全局变量、函数和节点访问等。

Creating an extension also makes for a better separation of code that is executed at compilation time and code needed at runtime. As such, it makes your code faster.
创建扩展还可以有效地分隔编译时和运行时代码。这样它可以让您的代码运行地更快。

Before writing your own extensions, have a look at the Twig official extension repository.
在创建您自己的扩展之前,请先看一下Twig官方扩展库

 

Create the Extension Class
创建扩展类

To get your custom functionality you must first create a Twig Extension class. As an example we will create a price filter to format a given number into price:
要实现定制功能,您首先必须创建一个Twig扩展类。作为示例,我们将创建一个价格过滤器,用以将给定数字转成价格格式:

  1. // src/Acme/DemoBundle/Twig/AcmeExtension.php 
  2. namespace Acme\DemoBundle\Twig; 
  3.  
  4. use Twig_Extension; 
  5. use Twig_Filter_Method; 
  6.  
  7. class AcmeExtension extends Twig_Extension 
  8.     public function getFilters() 
  9.     { 
  10.         return array
  11.             'price' => new Twig_Filter_Method($this'priceFilter'), 
  12.         ); 
  13.     } 
  14.  
  15.     public function priceFilter($number$decimals = 0, $decPoint = '.'$thousandsSep = ','
  16.     { 
  17.         $price = number_format($number$decimals$decPoint$thousandsSep); 
  18.         $price = '$' . $price
  19.  
  20.         return $price
  21.     } 
  22.  
  23.     public function getName() 
  24.     { 
  25.         return 'acme_extension'
  26.     } 
Along with custom filters, you can also add custom functions and register global variables.
除了定制过滤器,您还可以添加定制函数和注册全局变量。

 

Register an Extension as a Service
注册扩展服务

Now you must let Service Container know about your newly created Twig Extension:
现在您必须让服务容器知道您新建的Twig扩展:

  1. <!-- src/Acme/DemoBundle/Resources/config/services.xml --> 
  2. <services> 
  3.     <service id="acme.twig.acme_extension" class="Acme\DemoBundle\Twig\AcmeExtension"> 
  4.         <tag name="twig.extension" /> 
  5.     </service> 
  6. </services> 
Keep in mind that Twig Extensions are not lazily loaded. This means that there's a higher chance that you'll get a CircularReferenceException or a ScopeWideningInjectionException if any services (or your Twig Extension in this case) are dependent on the request service. For more information take a look at How to work with Scopes.
记住,Twig扩展并非被延迟加载。这就意味着如果您的服务(或本例中的Twig扩展)依赖request服务的话,您将有更高的几率得到CircularReferenceExceptionScopeWideningInjectionException 异常。详情请参见如何使用范围.

 

Using the custom Extension
使用定制扩展

Using your newly created Twig Extension is no different than any other:
使用您新建的Twig扩展与其它的没什么不同:

  1. {# outputs $5,500.00 #} 
  2. {{ '5500'|price }} 

Passing other arguments to your filter:
发送其它参数到您的过滤器:

  1. {# outputs $5500,2516 #} 
  2. {{ '5500.25155'|price(4, ',', '') }} 

Learning further
进一步学习

For a more in-depth look into Twig Extensions, please take a look at the Twig extensions documentation.
要更深入地了解Twig扩展,请查看Twig扩展文档