Skip to content

Instantly share code, notes, and snippets.

@aarsla
Last active July 26, 2017 07:20
Show Gist options
  • Save aarsla/f89f0863a251db9a5cff8faa1dc08a1b to your computer and use it in GitHub Desktop.
Save aarsla/f89f0863a251db9a5cff8faa1dc08a1b to your computer and use it in GitHub Desktop.
Twig Function - git commit/branch
<?php
namespace AppBundle\Twig\Extension;
class GitExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('gitVersion', array($this, 'gitVersion')),
);
}
/**
* Return current git version
*/
public function gitVersion()
{
$hash = exec('git rev-parse --short HEAD');
$branch = exec('git rev-parse --abbrev-ref HEAD');
$tag = exec('git describe');
return $tag. ' ' . $hash . ' on '.$branch;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'twig_git';
}
}
@aarsla
Copy link
Author

aarsla commented Jul 17, 2017

Usage: version: {{ gitVersion() }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment