/
TemplatingExtension.php
77 lines (68 loc) · 1.92 KB
/
TemplatingExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplatingExtension extends \Twig_Extension
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
public function getFunctions()
{
return array(
'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
'assets_version' => new \Twig_Function_Method($this, 'getAssetsVersion'),
);
}
/**
* Returns the public path of an asset
*
* Absolute paths (i.e. http://...) are returned unmodified.
*
* @param string $path A public path
* @param string $packageName The name of the asset package to use
*
* @return string A public path which takes into account the base path and URL path
*/
public function getAssetUrl($path, $packageName = null)
{
return $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
}
/**
* Returns the version of the assets in a package
*
* @param string $packageName
* @return int
*/
public function getAssetsVersion($packageName = null)
{
return $this->container->get('templating.helper.assets')->getVersion($packageName);
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'templating';
}
}