概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:42:15 | 143 次阅读 | 评论: 0 | 来源: 网络整理

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.

Create the Extension Class

注解

This cookbook describes how to write a custom Twig extension as of Twig 1.12. If you are using an older version, please read Twig extensions documentation legacy.

To get your custom functionality you must first create a Twig Extension class. As an example you’ll create a price filter to format a given number into price:

// src/AppBundle/Twig/AppExtension.php
namespace AppBundleTwig;

class AppExtension extends Twig_Extension
{
    public function getFilters()
    {
        return array(
            new Twig_SimpleFilter('price', array($this, 'priceFilter')),
        );
    }

    public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
    {
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
        $price = '$'.$price;

        return $price;
    }

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

小技巧

Along with custom filters, you can also add custom functions and register global variables.

Register an Extension as a Service

Now you must let the Service Container know about your newly created Twig Extension:

  • YAML
    # app/config/services.yml
    services:
        app.twig_extension:
            class: AppBundleTwigAppExtension
            public: false
            tags:
                - { name: twig.extension }
    
  • XML
    <!-- app/config/services.xml -->
    <services>
        <service id="app.twig_extension"
            class="AppBundleTwigAppExtension"
            public="false">
            <tag name="twig.extension" />
        </service>
    </services>
    
  • PHP
    // app/config/services.php
    use SymfonyComponentDependencyInjectionDefinition;
    
    $container
        ->register('app.twig_extension', 'AppBundleTwigAppExtension')
        ->setPublic(false)
        ->addTag('twig.extension');
    

注解

Keep in mind that Twig Extensions are not lazily loaded. This means that there’s a higher chance that you’ll get a ServiceCircularReferenceException 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.

Using the custom Extension

Using your newly created Twig Extension is no different than any other:

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

Passing other arguments to your filter:

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

Learning further

For a more in-depth look into Twig Extensions, please take a look at the Twig extensions documentation.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务