Skip to content

Commit

Permalink
add the new property 'version' for define asset version
Browse files Browse the repository at this point in the history
  • Loading branch information
ke20 committed Oct 30, 2013
1 parent e639a35 commit b26352e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
53 changes: 42 additions & 11 deletions Entea/Twig/Extension/AssetExtension.php
Expand Up @@ -7,31 +7,62 @@

namespace Entea\Twig\Extension;

class AssetExtension extends \Twig_Extension
class AssetExtension extends \Twig_Extension
{
private $app;
private $options;


private $_assetDirectory;
private $_assetVersion;

/**
* Constructor
*
* @param \Silex\Application $app
* @param array $options
*/
function __construct(\Silex\Application $app, array $options = array())
{
$this->app = $app;
$this->options = $options;
}


public function initRuntime(\Twig_Environment $environment)
{
parent::initRuntime($environment);

$this->_assetDirectory = $this->app['request']->getBasePath();
if(isset($this->options['asset.directory']))
$this->_assetDirectory = $this->options['asset.directory'];

$this->_assetVersion = '1.0';
if(isset($this->options['asset.version']))
$this->_assetVersion = $this->options['asset.version'];
}

public function getFunctions()
{
return array(
'asset' => new \Twig_Function_Method($this, 'asset'),
'asset' => new \Twig_Function_Method($this, 'asset'),
);
}

public function asset($url)

/**
* Logic for the "asset" function of Twig
*
* @param type $url
* @param type $version
* @return type
*/
public function asset($url, $version=NULL)
{
$assetDir = isset($this->options['asset.directory']) ?
$this->options['asset.directory'] :
$this->app['request']->getBasePath();

return sprintf('%s/%s', $assetDir, ltrim($url, '/'));
if($version !== NULL)
$this->_assetVersion = $version;

return sprintf('%s/%s?v=%s',
$this->_assetDirectory,
ltrim($url, '/'),
$this->_assetVersion);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -31,9 +31,14 @@ And then, in your twig file:
```
{{asset('/style/hello.css')}}
```
or with a specific version which overrides the global version
```
{{asset('/style/hello.css', '1.2')}}
```

### Properties
- **asset.directory**: Your asset directory
- **asset.version**: Your asset global version (default: 1.0)

### Installation

Expand Down

0 comments on commit b26352e

Please sign in to comment.