Przeglądaj źródła

[TwigBundle] added a yaml filter

Fabien Potencier 14 lat temu
rodzic
commit
17c500e0f0

+ 22 - 0
src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php

@@ -7,6 +7,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Bundle\TwigBundle\TokenParser\IncludeTokenParser;
 use Symfony\Bundle\TwigBundle\TokenParser\UrlTokenParser;
 use Symfony\Bundle\TwigBundle\TokenParser\PathTokenParser;
+use Symfony\Component\Yaml\Dumper as YamlDumper;
 
 /*
  * This file is part of the Symfony package.
@@ -42,6 +43,16 @@ class TemplatingExtension extends \Twig_Extension
         return $this->templating;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getFilters()
+    {
+        return array(
+            'yaml' => new \Twig_Filter_Method($this, 'yaml'),
+        );
+    }
+
     /**
      * Returns the token parser instance to add to the existing list.
      *
@@ -82,6 +93,17 @@ class TemplatingExtension extends \Twig_Extension
         );
     }
 
+    public function yaml($input, $inline = 0)
+    {
+        static $dumper;
+
+        if (null === $dumper) {
+            $dumper = new YamlDumper();
+        }
+
+        return $dumper->dump($input, $inline);
+    }
+
     /**
      * Returns the name of the extension.
      *